This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 cheers nik
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:
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.
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.
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.