The Java value "null" means "no object". A null reference is one that explicitly doesn't point to any object. If you try to access any instance member through a null reference, the JVM will throw a NullPointerException, which simply means "Hey, there's no object there!"
If a variable might be null, you need to test for that in your code. As in:
This will avoid triggering a NullPointerException. Finally, note that, in general, explicitly catching NullPointerExceptions is very bad form -- such an exception represents a programmer error, so fix the error, don't just wait for the error report! [ November 05, 2005: Message edited by: Ernest Friedman-Hill ]
Look at the line where the error occurs, and look at every object which might possibly be called or used on that line. You should be able to work out the line number from the stack trace which prints out when you thor your exception. For evey object which might or might not exist on that line: look at where it has been created. Has it actually been created? If it actually has been created, look carefully to see whether there is any possibility you have destroyed it anywhere.
As Mr Friedman-Hill says, a NullPointerException represents an error in programming somewhere, and you have to find out where.
If you cannot work out where it has occurred, count all the objects in the line mentioned, let's call them obj1 obj2 obj3 obj4. Then, if you still can't work out what has gone wrong, you can try a breakpoint and then examining every object in sight on a debugger. Are any of them null? An alternative to using the debugger is to insert a statement immediately before the offending line, reading something like this:- The printf() method calls the toString() method which exists explicitly or implicitly (as inherited from java.lang.Object) in every object from every class. If you get a print-out rather like this: Obj1: Name: Campbell Ritchie Obj2: Class2@1234abcd Obj3: Class3@2345def Obj4: null, you know which object is null, then you can follow back to its creation and see what has gone wrong.
You should hardly ever catch a NullPointerException, unless you have a method with the possibility of returning null, in which case the /** javadoc comment */ for that method ought to tell you about the possibility of returning null or a NullPointerException.
BTW: In my view there are some other exceptions which represent similar programmer errors and require alteration of the code, not to catch them, but to prevent them, including ...OutofBoundsException (the commonest in my experieonce being ArrayIndexOutOfBoundsException) and ArithmeticException (usually caused by trying to divide an integer number by zero.
Originally posted by Ernest Friedman-Hill: The Java value "null" means "no object". A null reference is one that explicitly doesn't point to any object. If you try to access any instance member through a null reference, the JVM will throw a NullPointerException, which simply means "Hey, there's no object there!"
If a variable might be null, you need to test for that in your code. As in:
This will avoid triggering a NullPointerException. Finally, note that, in general, explicitly catching NullPointerExceptions is very bad form -- such an exception represents a programmer error, so fix the error, don't just wait for the error report!
[ November 05, 2005: Message edited by: Ernest Friedman-Hill ]
hi,friedman
Even though if use this code
line 1:String length = response.getParameter("length"); line 2:if (length != null) { line 3: // Do something with "length" }
In line1 if the length has got no obj then it will definitely throws an exception
am i right if not please explain me sir
thanx
cinux
Seb Mathe
Ranch Hand
Joined: Sep 28, 2005
Posts: 225
posted
0
No, the exception will not be thrown at line 1, because String length = null; is a valid statement.
However, if you have another statement, like lenght.toUpperCase(); A NullPointerException will be thrown.
Regards,<br />Seb<br /> <br />SCJP 1.4
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Originally posted by saikrishna cinux:
This will avoid triggering a NullPointerException. Finally, note that, in general, explicitly catching NullPointerExceptions is very bad form -- such an exception represents a programmer error, so fix the error, don't just wait for the error report!
[ November 05, 2005: Message edited by: Ernest Friedman-Hill ]<hr></blockquote>
hi,friedman
Even though if use this code
line 1:String length = response.getParameter("length"); line 2:if (length != null) { line 3: // Do something with "length" }
In line1 if the length has got no obj then it will definitely throws an exception
am i right if not please explain me sir
thanx
cinux[/QB]
There are only two possible ways that your code above can throw a NullPointerException: 1) that response is referring to null. 2) that response(HttpServletREesponse?).getParameter(String) method is defective.
Using my personal experience only, option 1 is far more likely than option 2. That you have provided misinformation is more likely than both option 1 and option 2. That is, the code you have provided does not actually throw the NullPointerException. Instead, I suggest you provide the stack trace, including the exception message, and the source code that corresponds to that stack trace i.e. that produced it.
This way it is most definitely and systematically diagnosable, with a few exceptions: 1) You call an API that does not document its intention to throw a NullPointerException - unfortunately, the core API even does this in places. 2) You call an API that is a defective implementation. These 2 scenarios are less likely to occur than otherwise.