Get your CodeRanch badge!*
The moose likes Java in General and the fly likes please explain null pointer dereference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "please explain null pointer dereference" Watch "please explain null pointer dereference" New topic
Author

please explain null pointer dereference

J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

I've been searching for an explanation of this message and can't find anything that clearly explains it. For instance, what's wrong with this code?


The FindBugs tool is telling me that line 5 contains a null pointer dereference to the id variable but I don't see the problem. It's simply a check to make sure the variable is not null. Why is that a problem?


"There is no reason for any individual to have a computer in his home" ~ Ken Olson, Co-founder of DEC, 1977
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

I don't see a problem in line 5. But I do see a problem in line 9:

When id == null, you will get a NullPointerException in this line, because then you'll be trying to call length() on a variable that's null.

You probably meant:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Jk Robbins wrote:The FindBugs tool is telling me that line 5 contains a null pointer dereference to the id variable but I don't see the problem. It's simply a check to make sure the variable is not null. Why is that a problem?
I suspect that 'line 5' is actually line 9, and it's trying to tell you that
if (id != null || id.length() > 0) {
should be
if (id != null && id.length() > 0) {
(at least I assume so; it's certainly an error)

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

Thanks, you are correct, I meant line 9 and I see the error now. But what exactly does it mean to "dereference a null pointer"? Does it just mean failing to correctly check if a value is null?

I keep finding explanations like this which are not very helpful.


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Jk Robbins wrote:Thanks, you are correct, I meant line 9 and I see the error now. But what exactly does it mean to "dereference a null pointer"? Does it just mean failing to correctly check if a value is null?

Basically, yes. Also, the term 'pointer' is bad (but maybe it comes from the FindBugs tool): Java doesn't have pointers, it has references. In C/C++ speak, 'dereferencing' simply means getting the object that a pointer points to; in Java there's not really any such distinction.

Winston
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Jk Robbins wrote:Thanks, you are correct, I meant line 9 and I see the error now. But what exactly does it mean to "dereference a null pointer"? Does it just mean failing to correctly check if a value is null?

Dereferencing a reference means accessing the object it's referencing/pointing to. Most commonly, that means accessing a property or calling a method on the object.

So "dereferencing a null pointer" means trying to do something to the object that it's pointing to. Since it's not pointing to anything (because that's what null means), that's an error.

Note that you can copy references without accessing the object it references. That's why it's perfectly OK to assign null to variables or pass null into a method. But as soon as you use that "." afterwards, you're dereferencing it, and you'll get the exception.
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632
    
    7

Thanks to both of you; that's much clearer now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain null pointer dereference
 
Similar Threads
dynamic table creation problem calling onchange
Custom Component : Missing Update Model Phase?
Need to Hide Table inside div:Code is Working Fine in FireFox but Not in IE..Please Help
about checking values between rows with dynamic table created using java script
spaces while populating using s:iterator