wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes what is a nullpointerexception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is a nullpointerexception" Watch "what is a nullpointerexception" New topic
Author

what is a nullpointerexception

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
hi,

actually what is a NullPointerException and why it occurs
If occured what should be done to prevent it?


please if u have any resources please send me a link


thanx in advance


cinux


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Here's a really good example.

Me to my wife: "Hey Honey, can you pass me the 100 Million Dollar Bill?"

That will definitely throw a NullPointerException.



Mark
[ November 05, 2005: Message edited by: Mark Spritzler ]

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
But what if she only has two $50000000 bills?
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
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"
}

we cannot stop displaying the NullPointerException in the webpage

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
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
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"
}

we cannot stop displaying the NullPointerException in the webpage

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.

You could, of course, attempt to diagnose it yourself. Here is the process to do just that: http://www.tmorris.net/pubs/npe/


Tony Morris
Java Q&A (FAQ, Trivia)
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
thanx tony for providing me a better link for understanding NullPointerException

thanx
bye
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
Real world analogy: Sort of like trying to call someone without knowing their phone number.
Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
I hope that Mr.saikrishna cinux will not think that one should NEVER catch NullPointerExceptions.

If I want to use an image as a background for a home-made component that extends JPanel,
then this code looks realistic to me:

Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
OOPS

code should be read like this
public void paintComponent(Graphics g){

if (myImage!=null){//if image exists

g.drawImage(myImage,0,0,getWidth(),getHeight(),this);//show it

};

//...show rest on top

}

sorry!!!
 
 
subject: what is a nullpointerexception
 
Similar Threads
applet run on one machine but throw an exception on the other
Tech Word Game
BMP ejbRemove() problem
fruitmachine example doesn't work on real device
What is NullPointerException