null : Java Glossary
home N words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
null
null is the reserved constant used in Java to represent a void reference i.e a pointer to nothing. Internally it is just a binary 0, but in the high level Java language, it is a magic constant, quite distinct from zero, that internally could have any representation.

If you execute some code like this:

x.doSomething();
and x is null, you will raise a java.lang.NullPointerException. Note NullPointerException, not Null ReferenceException.

The difference between a null reference and a reference to an empty object, (e.g. a freshly minted default object, empty collection or empty Iterator) is a major headache in Java. When dealing with other people’s code I want to strangle them. Why won’t they tell me when a method might return null or empty? Why won’t they tell me if a method can’t accept a null or empty object?

if ( x != null )
   {
   x.doSomething();
   }
must be the most common Java code fragment. I wish there were some more elegant way to handle this, that gives you the speed and space conservation of null, without the coding overhead bubblgum of checking for it at every turn. I have five solutions:
  1. Don’t return Lists from your methods; return Iterators instead. This way you can return the empty Iterator. The Iterator’s consumer does not need to deal specially with the null case. You don’t need the overhead of cloned empty List objects.
  2. Create a singleton empty prototype object for each class that you pass around to act like null. The catch is, it may actually need to be a subclass so that its methods don’t actually do anything. You have to make sure nobody modifies the empty object. You can safely call methods on this object and nothing will happen. You can safely retrieve its empty/default-value fields. You would need an amanuensis to compose the code for such objects. The effort of composing them manually would be even more work than dealing with null. Using this technique would be more robust however. You would make all methods final so that they would inline down to nothing. This would be even faster than checking explicitly for null! Null is typeless. Empty objects are typed. This may or may not be considered a Good Thing™.
  3. New Java syntax to insert if (x != null) more tersely: e.g. t!.doit(); is the same as if (t != null) t.doit();.
  4. Use rigid Javadoc conventions to track whether a method consumes/produces null references, empty objects, or blank Strings.
  5. Simply ignore java.lang.NullPointerExceptions. The catch is, there is no way to do that in Java. You can’t just ignore them and return to the code just after the call. You can’t turn them off. This would require a new language where NullPointerExceptions are simply treated as no-ops.

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 53,802. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/null.html J:\mindprod\jgloss\null.html