File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is a nullpointerexception

 
saikrishna cinux
Ranch Hand
Posts: 689
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what if she only has two $50000000 bills?
 
saikrishna cinux
Ranch Hand
Posts: 689
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
saikrishna cinux
Ranch Hand
Posts: 689
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx tony for providing me a better link for understanding NullPointerException

thanx
bye
 
Adam Richards
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Real world analogy: Sort of like trying to call someone without knowing their phone number.
 
Ko Wey
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic