If you execute some code like this:
x.doSomething();and x is null, you will raise a java.lang.NullPointerException. Note Null PointerException, 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:
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.58] | |||
| You are visitor number 81,635. | |||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror) | ||
| http://mindprod.com/jgloss/null.html | J:\mindprod\jgloss\null.html | ||