• 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

on null pointerexception

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I wanted to know how to handle the NullPointerException in Servlets?

in service method we can give...like...

public void service() throws servletException,IOException{
//write your code...
}

thanks in advance
regards
amit bhadre
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Null Pointer Exception is RuntimeException. You don't want to catch it and rethrow it instead you may want to refactor your code to throw application exception by checking for null where that object should not be null.

you can put your code in try-catch block.
[ September 06, 2006: Message edited by: Meghana Meduri ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get the NullPointerExceptions when you try to refer to un-initialized references. Make sure all your references are initialized. If you are reading the request parameters make sure you perform a null check before using the parameter value.

i.e.:


or more effectively;


In the above code I use an empty string to avoid null refernce.

You can't throw NullPointerExceptions around because they are Runtime Exceptions and should never occure if you take necessary care to avoide them.
[ September 06, 2006: Message edited by: Isuru Sampath ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Meghana Meduri:
you may want to refactor your code to throw application exception



I disagree, a null pointer exception is almost always the result of a bug in your code. You should not try to catch them. Rather let them propogate out to the cointainer so that it can be reported loud and clear. That way, you find and fix the bugs.

Your web.xml can be used to direct the errors to a servlet where you can report the exception in an application-specific manner.
 
Meghana Meduri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear,
I too agree that getting NULLPointerException is bug in a code.
By refactoring code I meant to look through the different scenarios and check for null so that code will not through null pointer exception.
By throwing application exception I meant a scenario where if value of a specific object is null where as it should not, then I would check for null and give a gracefull error message back to the user by throwing application exception.

In Short,
Sometimes having null is valid scenario where application should just ignore it and sometimes it is not, In which case I would throw a Application exception by checking for null.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed, my point was that they should not be caught or masked.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:


I disagree, a null pointer exception is almost always the result of a bug in your code. You should not try to catch them. Rather let them propogate out to the cointainer so that it can be reported loud and clear. That way, you find and fix the bugs.

Your web.xml can be used to direct the errors to a servlet where you can report the exception in an application-specific manner.



I think, this is true for all RunTimeException...

Right?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic