• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
krisnav,
Please read theJavaranch naming policy and change your display name to one that conforms if you wish to continue posting here. Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic