wood burning stoves 2.0*
The moose likes Servlets and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

mike zhang
Ranch Hand

Joined: Feb 26, 2002
Posts: 59
The following simple code gives a NullPointerException plus the right answer. I just don't know why it gives the null pointer exception
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String name = request.getServerName();
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println("server = " + name);
}
the output is:
java.lang.NullPointerException
server = XXXX (correct)
The serverlet API I use is 2.1 and I use Jrun 2.3 on apache.
Thanks,
Mike
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
To track this down you could enclose all of the doGet statements in a try / catch and have the exception print a stack trace to System.err.
Bill
mike zhang
Ranch Hand

Joined: Feb 26, 2002
Posts: 59
The interesting thing is, if I put all these few lines into a try {} block as follows:
try {
String name = req.getServerName();
PrintWrite out = res.getWriter();
out.println("server = " + name);
} catch(ServletException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
It works well,i.e. it only shows the correct server name without the "java.lang.NullPointerException" msg.
Especially the last "catch" of "Exception e" makes the difference. I just don't understand what exactly caused the different output ???
Mike
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Because...

NullPointerException is not a ServletException, nor an IOException, so the generic 'Exception' is the one to catch it.

When you say e.printStackTrace() that will print the exception out to the standard error, which is not the same as System.out (many times it is, but other times it can be different... for example, a servlet container would probably direct System.err to an error log file)

When you catch the error, it won't bubble up to the System.out OR the System.err until you print it out to either one of those.

When you don't catch it, the servlet container will receive it, the container should construct a ServletException, and then that gets thrown out to the end user, which you will see.
mike zhang
Ranch Hand

Joined: Feb 26, 2002
Posts: 59
Mike, thanks again for your good points. Now I understand how container handles and reacts. But, what exactly causes the NullPointerException in that few lines code ??? I can't find what's wrong in that three lines.
Thanks,
Mike
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I admit, I can't see anything in that code that would give a NullPointerException, *given that you have server= XXXX on the output*.

If req was null, then XXXX wouldn't print out.
If res was null, then you wouldn't get an 'out'
If out was null, you wouldn't get "server = " OR the XXXX.

Is this part of a Servlet? or a helper class? Could the exception be from some other part? What do you get if you code
mike zhang
Ranch Hand

Joined: Feb 26, 2002
Posts: 59
If I do
catch(Exception e) {
e.fillInStackTrace(); e.printStackTrace();}
}
it works fine without giving any error. (just shows server = ....).
so anyway I will just catch the "Exception".
Thanks,
Mike
krishna valavala
Greenhorn

Joined: Mar 06, 2002
Posts: 10
I am guessing your name is null. Again, it puzzles me, when there is an excpetion in middle of the processing how did it recover. In other words when you get an exception, the processing should stop, and nothing be displayed afterwards.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
krisnav,
Please read theJavaranch naming policy and change your display name to one that conforms if you wish to continue posting here. Thanks.


Matthew Phillips
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException
 
Similar Threads
eclipse issue
How to get environment variable?
eclipse configuration
isDirectory command doubt ?
SWT: app close when trying to change text.setText()