473,626 Members | 3,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NullPointerExce ption a.k.a NullReferenceEx ception

13,262 MVP
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it before, lending proof to Einstein's statement: “Only two things are infinite, the universe and human stupidity ...”.

Main Cause

Dereferencing null.

This is by far the more common cause of getting the exception.

Reference types in both Java and .NET have a default value of null. A variable of a reference type whose value is null indicates that that variable is pointing to no object. Nothing. You can do very little with nothing. You certainly can't dereference it without summoning the NullPointer exception to come and smack you with you the stupidity label. So if we have
Expand|Select|Wrap|Line Numbers
  1. Type var = null;
We have created a variable of type Type, named it var, and made it point to no object. If we dot (apply the dereferencing operator(.)) var we are going to get the NullPointer exception because we will have dereferenced no object. We have asked nothing to do something. Very stupid indeed. var needs to be pointing to some non-null object for us to be able to dereference it. Possible solutions are
Expand|Select|Wrap|Line Numbers
  1. var = new Type();
or
Expand|Select|Wrap|Line Numbers
  1. var = anotherVar;
where anotherVar is a reference to a previously initialized object.

This all sounds very simple doesn't it? Here are a few tips on how to avoid it.

Avoiding it


1.) Always declare variables closest to where they are used.

The old C way of declaring variables at the top leaves room for forgetting to initialize them before using them several lines later in the code. You don't get the NullPointer exception in C if you do forget. You simply get garbage.

2.) Careful with composites.

A statement such as
Expand|Select|Wrap|Line Numbers
  1. Type[] array = new Type[5];
creates a variable of type Type[], names it array and initializes it to an array object with 5 elements. The five elements in this array array[0], array[1], ..., array[4] unfortunately have default values of the Type type. If Type is a reference type then these values are all nulls. They each point to no object. Dereferencing any of array's indexes will cause the NullPointer exception to be thrown. Each of the five indexes of array need to made to point to non-null values before they can be dereferenced. Obviously the same holds for other collections and composite types.

3.) Beware of method/constructor variable hiding.

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Person {
  3.     String name;
  4.  
  5.     public Person() {
  6.         String name = "Something Stupid";
  7.     }
  8.  
  9.     public String getFirstName() {
  10.         return name.split(" ")[0];
  11.     }
  12.  
  13.     public static void main(String[] args) {
  14.         Person stupid = new Person();
  15.         System.out.println(stupid.getFirstName());
  16.     }
  17. }
  18.  
In the code snippet above, the Person class has a name property which the constructor attempts to initialize. The attempt is hopeless. Instead a new name variable is created inside the constructor. The declaration of this variable hides the name property of the class inside that constructor. To add further insult, that name variable declared inside the constructor is only available inside that constructor. It is out of scope anywhere else outside that constructor.
When the unsuspecting main method calls the getFirstName method, the split is going to be called on a non initialized property. No prizes for guessing what ensures.
In general, care is needed when accessing instance variables that are not initialized when they are declared and local variables are best given names that do not clash with instance variable names.

4.)Read the relevant documentation.

Programmers are allowed to explicitly throw the NullPointer exception in their code for whatever reason they deem appropriate. This means that you can get the NullPointer exception without explicitly dereferencing a null variable but by executing code whose execution flow passes through a statement with a throws clause for the NullPointer exception.
A simple Java example is the java.util.Hasht able's contains method. The code for that method is
Expand|Select|Wrap|Line Numbers
  1.  public synchronized boolean contains(Object value) {
  2.     if (value == null) {
  3.         throw new NullPointerException();
  4.     }
  5.  
  6.     Entry tab[] = table;
  7.     for (int i = tab.length ; i-- > 0 ;) {
  8.         for (Entry<K,V> e = tab[i] ; e != null ; e = e.next) {
  9.         if (e.value.equals(value)) {
  10.             return true;
  11.         }
  12.         }
  13.     }
  14.     return false;
  15.     }
  16. /*
  17.  * @(#)Hashtable.java    1.116 06/05/26
  18.  *
  19.  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  20.  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  21.  */
  22.  
As you can see here the NullPointerExce ption is explicitly thrown if the parameter passed is null. Fortunately, we don't always have to guess which methods throw the nullpointer exception under what conditions. The documentation of the methods should explicitly tell us what conditions cause the method to throw that exception. This is always the case in well documented code and you should likewise document your code properly if you explicitly throw any exceptions under certain conditions.
So if we want to invoke the contains method of the Hashtable class with a parameter p, reading the documentation of that method tells us that we should check that our p is not null before invoking the method.

The same holds for JSP parameters and attributes. If a parameter is requested by the request.getPara meter method and no parameter with that name exists in the request then null is returned. Any operations on the returned parameters may cause the NullPointer exception if the returned value is not checked for non null.

5.)Log whenever possible during development

Often enough, the exception is caused by falsely assuming certain data values and certain paths of execution. Sprinkling some print outs at critical positions will help to dispense some of those false assumptions and expose the holes where the nullPointer is most likely to try and sneak in through. Indeed I will go as far as saying that setting up the logging framework should be one of the first steps that should be done during system implementation.


6.) Beware of super class constructor initializations vs derived class instance initializers

There is another less obvious way of creating nulls all over the place. Indeed the following discussion may not be for the faint hearted :

In the snippet below ( especially prepared for our amusement by Jos)
Expand|Select|Wrap|Line Numbers
  1. abstract class BaseClass {
  2.  
  3.     abstract protected void initialize(String name);
  4.     public BaseClass(String memberName) {
  5.         initialize(memberName);
  6.         System.out.println("in base: " + this);
  7.     }
  8. }
  9. class NullMember extends BaseClass {
  10.     String    name    = null;
  11.     public NullMember(String memberName) {
  12.         super(memberName);
  13.     }
  14.  
  15.     @Override
  16.     protected void initialize(String name) {
  17.         this.name = name;
  18.     }
  19.  
  20.     @Override
  21.     public String toString() {
  22.         return name;
  23.     }
  24.  
  25.     public static void main(String[] args) {
  26.         NullMember nm = new NullMember("foo");
  27.         System.out.println("NullMember: " + nm);
  28.     }
  29. }
  30.  
The output is
Expand|Select|Wrap|Line Numbers
  1. in base: foo
  2. NullMember: null
  3.  
It appears in the main method that the variable name was never initialized even though the print outs do show that name indeed was “foo” at some point. This peculiarity is due to the order of execution of statements during object construction. The JLS (8.8.7) explains this as
Let C be the class being instantiated, let S be the direct superclass of C, and let i be the instance being created. The evaluation of an explicit constructor invocation proceeds as follows:
•First, if the constructor invocation statement is a superclass constructor invocation, then the immediately enclosing instance of i with respect to S (if any) must be determined. Whether or not i has an immediately enclosing instance with respect to S is determined by the superclass constructor invocation as follows:
<Here the JLS mumbles on in it’s lifeless tone to explain how to determine the immediately enclosing instance of i. Thankfully we are not concerned with any of that in this discussion>

• Second, the arguments to the constructor are evaluated, left-to-right, as in an
ordinary method invocation.
• Next, the constructor is invoked.
• Finally, if the constructor invocation statement is a superclass constructor
invocation and the constructor invocation statement completes normally, then
all instance variable initializers of C and all instance initializers of C are executed.
The last bit there is what we are interested in. If we are calling a super class constructor, any initializations that we will do in that constructor will be overwritten by calls to instance initializers in the calling (extending) class soon after the super class constructor is called. If the initializers in the inheriting class set any variables to null as in the example above, then we are treading in a field were some nullpointers have been set.
That said, there really isn’t much to gain at all by explicitly initializing instance fields to null. If we don’t provide it then we have left no explicit initializers to be used to overwrite all the good work that our super class constructor will have done.

Fortunately, this peculiarity does not exist for .NET programmers.
The equivalent C# example
Expand|Select|Wrap|Line Numbers
  1. abstract class BaseClass
  2.     {
  3.         public abstract void initName(String s);
  4.         public BaseClass(String s)
  5.         {
  6.             initName(s);
  7.             Console.WriteLine("in base "+this);
  8.         }
  9.  
  10.     }
  11.     class NullMember : BaseClass
  12.     {
  13.         String name = null; 
  14.        public NullMember(String s):base(s)
  15.         {
  16.  
  17.         }
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.             NullMember p = new NullMember("foo");
  22.             Console.WriteLine("NullNumber "+p);
  23.             Console.ReadLine();
  24.         }
  25.         public override String ToString()
  26.         {
  27.             return name;
  28.         }
  29.         public override void  initName(String s)
  30.         {
  31.             this.name = s;
  32.         }
  33.     }
  34.  
is well behaved, printing “foo” both in the super class constructor and in the main method.
This behavior has the blessing of the C# Language specification (10.11.2) which says
When an instance constructor has no constructor initializer, or it has a constructor initializer of the form base(...), that constructor implicitly performs the initializations specified by the variable-initializers of the instance fields declared in its class. This corresponds to a sequence of assignments that are executed immediately upon entry to the constructor and before the implicit invocation of the direct base class constructor. The variable initializers are executed in the textual order in which they appear in the class declaration.
The variable initializers are executed before invoking the base class constructor meaning that the name = null part is the one that is overwritten by the constructor call and not the other way around.


Debugging it.

If you are getting this exception then you probably have a stacktrace listing the methods and or constructors that were invoked up to and including the exact line where this exception was thrown. The first thing to do with that trace is to identify the first line in the stack trace which comes from your code. That line will contain either an explicit dereference of a null variable of a call to some block of code which induces the nullPointer exception.
The first case is easy. Just test each of the dereferenced variables on that line to make sure that they are not null. The second case involves checking the documentation of the code you have invoked to understand what conditions will cause it to throw the exception and then to compare those conditions with the conditions in which you are invoking it.

Demonstrating with a Java example,
Expand|Select|Wrap|Line Numbers
  1. static Object key;
  2. static Hashtable<String, String> t = new Hashtable<String, String>() {
  3.     {
  4.         put("one", "entry");
  5.     }
  6. };
  7. public static void main(String[] args) {
  8.     System.out.println(t.contains(key));
  9. }
  10.  
you would get
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" java.lang.NullPointerException
  2.     at java.util.Hashtable.contains(Hashtable.java:265)
  3.     at Person.main(Person.java:20)
  4.  
The first line that belongs to my code is Person.main(Per son.java:20).
On that line I have
Expand|Select|Wrap|Line Numbers
  1. System.out.println(t.contains(key));
  2.  
Variables being dereferenced are t and out both of which are not null. The block of code I am invoking there is java.util.Hasht able's contains method and the condition that causes it to throw the NullPointer exception is if the parameter passed is null. My parameter, key, is never initialized to a non null value so that is the problem.
Of course stepping through with a debugger would lead one to the same conclusions.

Conclusion

Having read this article you may be enticed to think that you will never get the NullPointer or NullReference exception again. Such thinking is encouraged of course. It would be nice to prove Einstein wrong again.
Mar 9 '09 #1
1 17541
JosAH
11,448 Recognized Expert MVP
I'm closing this thread; any discussions about NullPointerExce ptions please post them in the 'answers' section of the Java forum.

kind regards,

Jos
Aug 17 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
8570
by: K S Aldebaraan | last post by:
I'm trying to submit a form with an action of a servlet, and a view equal to the same jsp page. I'm not sure what I'm doing wrong, but keep getting a NullPointerException on the second line of code below: RequestDispatcher dispatcher = myConfig.getServletContext().getRequestDispatcher(view); dispatcher.forward(request, response); The funny thing is that it will do most of the database work it's supposed to, but then gives me the...
4
4066
by: gabryh | last post by:
Hi, The following code throws me NullPointerException. ..... public static boolean isEmpty(String value) { return ((value == null) || (value.trim().equals(""))); }
0
4409
by: Old-timer | last post by:
Not sure where else to post this. I'm sure I'm doing something wrong, but I wouldn't think a simple app would give me so much trouble. I've got a small test java class that I'm trying to have show up via tomcat. I'm running on Fedora Core 2, tomcat-4.1.27-13. I haven't noticed any difference if I start apache httpd-2.0.50-2.1 or not. Here are the errors: MsgAjp - -BAD packet signature 18245
2
3075
by: Smith | last post by:
The program compiled successfully, but it gives the following error on runtime.... java.lang.NullPointerException at FrogManiaApp.paint(FrogManiaApp.java:102) at sun.awt.RepaintArea.paint(RepaintArea.java:177) at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260) at java.awt.Component.dispatchEventImpl(Component.java:3678) at java.awt.Container.dispatchEventImpl(Container.java:1627)
13
1961
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try{
1
2434
by: ketand1 | last post by:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; import java.lang.*; class DbAwt extends Frame implements ActionListener { private TextField t1, t2, t3;
2
2273
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to Events -> Action -> actionPerformed. The auto-generated code appears for a brief moment before it...
1
3186
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to Events -> Action -> actionPerformed. The auto-generated code appears for a brief moment before it...
3
3328
by: chris123456789 | last post by:
Hi, when I run my code I get a NullPointerException:null. Here is the part of the code where the error occurs: import java.util.*; import java.io.*; public class Decrypt { ArrayList<String> dictionary; ArrayList<String> encryptedText;
0
8272
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8644
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.