How to handle NullPointerException in Java [closed]
How to handle NullPointerException
in Java? Please provide details so I can get rid of this problem
You should avoid NullPointerExceptions:
if(someObject != null) {
someObject.doSomething();
} else {
// do something other
}
Normally you should ensure that the objects which you use are not null.
You also can catch the NullPointerException and except using an if-condition.
try {
someObject.doSomething();
} catch(NullPointerException e) {
// do something other
}
Normally there is a bug in your code, when a NullPointerException occurs.
try {
// something stupid
} catch(NullPointerException e) {
// probably don't bother doing clean up
} finally {
// carry on as if nothing went wrong
}
You should really make yourself familiar with the concept of a variable being null. Checkout the API: http://java.sun.com/javase/6/docs/api/java/lang/NullPointerException.html
Generally, try to do more research in advance.
精彩评论