As a general rule, no, don't handle NPEs (NullPointerException). Let them propagate and work out what caused the Exception. Handling an NPE will simply allow the null to stay around and cause more trouble later on.
There isn't much you can do while handling the NPE. You just capture the NPE and using JVM stack trace, you modify your code to get rid of NPE.
Joined: Jul 19, 2009
Thanks Campbell and Dev for the sincire reoly.
Well, the idea behind asking this was, if code does not handle NPE externally then
without analyzing the code we can never say which object is actually null.
I got this during my project code, so i asked for the same.
if code does not handle NPE externally then without analyzing the code we can never say which object is actually null.
If it's not handled, your program will crash, and the full stack trace, with line numbers (if debug information is included), should be output. You can then tell where the NPE happened, and generally find which instance was null.