• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in my servlet - throws NullPointerException

 
Aditya Vasudeva
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet as follows:




The problem I face is that when the 2 parameters in the if statement do not match, error.jsp is called and works fine but, when the parameters match "aditya", a NullPointerException is thrown. The error reflected by the server says that the error is on the if statement(Line 20). The if statement returns Null instead of dispatching to welcome.jsp.

Waiting for a reply,
please help me as its frustrating.

Thank You.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens on line 20? Presumably its this line:


if((a.compareTo("aditya")==0) && (b.compareTo("aditya")==0)) {


so either username or password is null?
 
Aditya Vasudeva
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've even tried the equals() function of java.lang.String. It does'nt work even then ...

please help...
 
Aditya Vasudeva
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes...line 20 is the if statement.

the variable a contains username and b contains password.

If they are null, then how is the else condition working fine....???


please help...
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it is that line? Then either the variable a or b is null. Calling any method where the variable is null will give you a NullPointerException regardless of the mehtod you choose. You can check either are not null before performing your check. Or, sicne this is a web applicaiton, define an error page in web.xml.
 
Aditya Vasudeva
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if either a or b is null, then how does the else condition work fine???

is'nt request.getParameter() returning a string which is not matching "aditya" and hence error.jsp is called. How then can null be returned??

please help...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
What happens on line 20? Presumably its this line:



so either username or password is null?


hello, As Paul said, here this is the only possible!!!

why can not you test a,b wether they are null or not?

please try to debug the username and password
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is'nt request.getParameter() returning a string which is not matching "aditya" and hence error.jsp is called. How then can null be returned??

It will return null if the parameter does not exist in the request or is not set.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic