File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Problem in my servlet - throws NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem in my servlet - throws NullPointerException" Watch "Problem in my servlet - throws NullPointerException" New topic
Author

Problem in my servlet - throws NullPointerException

Aditya Vasudeva
Ranch Hand

Joined: May 27, 2008
Posts: 76
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

Joined: Apr 14, 2004
Posts: 10336

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?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Aditya Vasudeva
Ranch Hand

Joined: May 27, 2008
Posts: 76
I've even tried the equals() function of java.lang.String. It does'nt work even then ...

please help...
Aditya Vasudeva
Ranch Hand

Joined: May 27, 2008
Posts: 76
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: May 27, 2008
Posts: 76
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 14, 2004
Posts: 10336


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
 
subject: Problem in my servlet - throws NullPointerException