aspose file tools*
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
Bartender

Joined: Dec 16, 2010
Posts: 1043
    
  13

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?


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

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 8 API documentation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8223
    
  23

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?
Articles by Winston can be found here
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1043
    
  13

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: 8223
    
  23

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: 4465
    
    8

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
Bartender

Joined: Dec 16, 2010
Posts: 1043
    
  13

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