• Post Reply Bookmark Topic Watch Topic
  • New Topic

on null pointerexception

 
amit bhadre
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • 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
 
Meghana Meduri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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 ]
 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65518
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 65518
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed, my point was that they should not be caught or masked.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!