aspose file tools
The moose likes Beginning Java and the fly likes null confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null confusion" Watch "null confusion" New topic

null confusion

nik rb

Joined: Sep 25, 2005
Posts: 21
hi all im really confused about the 'null' concept .. does any1 kno a good article which tells me all theres to know about null??? please let me know anything you know about null and specially cases which lead to a nullpointerexception
Tommy Becker

Joined: Sep 22, 2005
Posts: 8
Null in Java isn't really that complicated, unlike nulls in a relational database. Basically null is the abscence of an object. If you have an object reference that does not point to an object, it is null. As for NullPointerExceptions, they occur when you attempt to call a method using a null object reference. For example:

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

A variable of reference type -- i.e., a variable that refers to any kind of object -- is like a dog leash. It can have a dog on the end of it, or it can have no dog. It can be attached to a dog sometimes, and no dog at other times -- or different dogs over time. Whether it has a dog or not, it's the same leash. "null" means "no dog." It's like an empty leash.

You get a NullPointerException any time you write (or some other code writes) "x.anything" if x is null. The only exception is if "anything" is a static method or variable, because in that case the value of "x" is actually not needed.

This is like what happens if you try to walk, feed, or pet the non-existent dog at the end of an empty leash.

You can read a longer discussion that uses a different metaphor here. That story talks about other kinds of Java variables too.

[Jess in Action][AskingGoodQuestions]
nik rb

Joined: Sep 25, 2005
Posts: 21
thanx that solved a lot of stuff... i guess just one further question will a reference become null only if it is assignedthe value null explicitly as in


or are there other ways that makes refrences null???

to reframe the question is the dog free only if i unleash it or there are other elements that may help break the shackles ??
"who let the dogs out.........."
Raistlin Zischer

Joined: Jul 05, 2005
Posts: 25
There are other ways where you can end up with a null object that could cause you to get a NullPointerException. The most common one I've seen is where you do a query on a database where no record is retrieved. Think of this as calling a dog kennel and ask for the wrong dog or vise-versa. The dog may exist but you asked for it the wrong way. Now, if you ASSUME you have retrieved the right record, or any record for that matter and try to do something like .getName(), this will throw your nullpointerexception. The best bet is to check to make sure you have gotten at least one record before trying to manipulate the query. Think of this as making sure you have a dog on the leash before you go walking it in the park.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As Raistlin points out, the null in the assignment does not have to be explicit. There are times when a method will return a null value. If you assign this return value to a reference variable, then the reference will be null. This is why it is important to check the documentation for any method that returns a reference. You need to be aware whether or not the method will return null.

However, notice that this still involves an explicit assignment, even if the null value is not so obvious. There is one place where an explicit assignment is not necessary: member variables. All member variables are initialized to a default value if you do not explicitly initialize them yourself. For reference variables, the default is null.

To avoid any problems this may cause, it is a good practice to explicitly initialize all class variables yourself. Even if you explicitly initialize it to null, then you are aware of what its value is.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: null confusion