NullPointerException is very common exception that every beginner encounters in learning phase, and experienced developer knows how to find the cause of null. To go ahead lets us see a brief overview of datatypes in Java.
Data types in Java can be categorized in two types i.e. Primitive Data type and Reference Data type.
Primitive data type consists of int, float, double, long, char etc…, size of such data type is fixed and don’t change. For e.g. int is of 4 bytes Reference data type don’t have fix size in terms of bytes, hence memory is dynamically allocated using the new operator, for e.g. String, User Defined Classes. So the reference type stores the reference of memory allocated for data.
Consider two statement in Java int x=10 and String y = “ABC”, int is primitive data type of 4 byte size. Fix size memory will be allocated and 10 will be stored. In case of String which is reference type memory will be allocated dynamically depending on the length of a string and a reference is stored. Following diagram will give a better understanding
We know there is no methods attached with primitive data type, but reference data type i.e. instance of an object will have methods with it. For e.g. we can call y.length() to get the length of the String.
When you assigned nothing to variable [ primitive or reference ] and try to use in a program, java will generate compile time error saying variable might not have been initialized.
As per Java Documentation NullPointerException arises in following circumstances :
- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
Think of following line of code
String y = null;
y = readSomething();
// Read Something will return data or null
In above case either the length of y will be printed or it will throw NullPointerException depending on what readSomething method returns.
Just to demonstrate Consider class A with get and set methods for setting the instance variable no.
Observe the setNo(Integer num) method which takes Integer as an argument.
Observe the main method where the setNo method is called with null value. [ Methods will be called from external factors and may pass null values ]
So now setNo method will generate NullPointerException as when it will try to convert num in to primitive no as num contains Null.
How to avoid NullPointerException ?
It is always better to check for null condition before doing some operations we can change setNo method implementation to check for null before using it.
//Ok proceed with num.
no = num;
//doSomething for null case
This kind of checking is always advisable where the object creation is not in control of program.
How to know where or why NullPointerException occuered ?
When we compile the the above program we will get something like stack trace from where we can find the cause.
We can see from stack trace the line no 14 and line no 23 are cause for NullPointerException, we can read it like from line 23 the call is made to line 14 in some way and line 14 is generating the NullPointerException.
Line 14 is no = num
Line 23 is x.setNo(a);
We can figure out the a is having a null value which is passed to in function call.
At the ends lets not forget that NullPointerException is an unchecked exception and it will always encounter at run time and not at compile time. Be cautious with using objects.