This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain null pointer dereference

 
J. Kevin Robbins
Bartender
Posts: 1725
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
J. Kevin Robbins
Bartender
Posts: 1725
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1725
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you; that's much clearer now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic