permaculture playing cards
The moose likes Servlets and the fly likes java.lang.NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "java.lang.NullPointerException" Watch "java.lang.NullPointerException" New topic


kevin schmidt
Ranch Hand

Joined: Aug 24, 2001
Posts: 85
My page is throwing the exception
"java.lang.NullPointerException". Now I was wondering, what am I doing for it to throw this. Here is a bit of the code:
if(request.getParameter("sort").equals("Date")) {
else if (request.getParameter("sort").equals("Date")) {
else {
catch(Exception e) {
out.println("<tr><td>Exception: " + e + "</td></tr>");
The initial if statement works, the else if works, but when the page has to resort to the else statement, then I get the java.lang.NullPointerException error. What can I do? TIA.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

if there is no such parameter as "sort", then the very first if statement will throw the nullpointerException.

And that's only the ones that I've replied to.
Brian Blesbok

Joined: Sep 03, 2001
Posts: 9
have a look at what you have posted here.
You are expecting us to try to tell you why you are getting an NPE in a section of code that you havent even given us.
First of all, the first statement will give an NPE if sort does not exist on the request, second, the 2nd statement is the *same* as the first.
Post the whole code if you want help
[This message has been edited by Brian Blesbok (edited September 14, 2001).]
BJ Grau
Ranch Hand

Joined: Jul 10, 2001
Posts: 234
Im with Mike on this. Without seeing the rest of your code I can guess that you are not checking if sort even exists before you are trying to call the equals method on it. If sort does not exist in the request, request.getParameter("sort") will return null and then you are trying to call equals on null which will throw a NullPointerException. NullPointerException was specifically made by the folks at Sun to tell us when we are trying to call a method on null.
Good luck.
[This message has been edited by BJ Grau (edited September 15, 2001).]
Dave Soto
Ranch Hand

Joined: Sep 15, 2001
Posts: 55
In general, you should have a little construct like this whenever you get a parameter.
String myParameter = request.getParameter("myParameter");
if (myParameter != null)
//write your code, make your comparison, whatever you like
//handle your error (the fact that myParameter is null)
I agree. Here's the link:
subject: java.lang.NullPointerException
It's not a secret anymore!