Scenario's
When we 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.
Example
package com.javalearning.crazy;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class AvoidNullPointerException {
String name = "Crazy";
public static String getClassName() {
return null;
}
public static AvoidNullPointerException getObject() {
return null;
}
public static List<String> getCollections()
{
List<String> a = new ArrayList<String>();
if("Blog".equals("Crazy"))
{
a.add("Programming");
return a;
}
return Collections.emptyList();
}
public static void main(String[] args) throws NumberFormatException,
IOException {
String havingNullValue = null;
if ("Passion".equals(havingNullValue)) {
System.out.println("Preventing Null Pointer Exception in String");
}
String className = getClassName();
if (className != null) {
System.out.println("Preventing Null Pointer Exception in Return value");
}
AvoidNullPointerException obj = getObject();
if (obj != null) {
System.out.println("Preventing Null Pointer Exception in Object");
}
String[] stringArray = new String[5];
stringArray[0] = new String("Dreams");
stringArray[1] = null;
for (int i = 0; i < stringArray.length; i++) {
if (stringArray[i] != null) {
System.out.println("Preventing Null Pointer Exception in Array Elements: "+stringArray[i]);
}
}
List<String> list = getCollections();
if(list != null){
System.out.println("Preventing Null Pointer Exception in Collection: "+list );
}
try {
if(getClassName().equals("Current"))
{
System.out.println("Class Names are equal");
}
} catch (NullPointerException npe) {
System.out.println("Preventing Null Pointer Exception through Exception Handling");
}
}
}
Preventing in String
Consider a scenario where we need to get string value at runtime and compare with constant value, based on that outcome we need to do some set of operation. Mostly we used to do like this
havingNullValue.equals(“Passion”)
It will throw null pointer exception when the run time object is null. We can prevent this by keeping constant string as left parameter. If we do like this it always access the initialized object which doesn't make any issue.
Preventing in Object
This scenario usually exist when we try to access object through a shared method, there is chance of null object return. Before calling instance method or accessing instance variable through that object we need to have proper null check condition to prevent null pointer Exception.
AvoidNullPointerException obj = getObject();
if (obj != null) {
obj.instanceMethod()
}
Preventing in Collection
When a method return list object mostly we used to code like, based on some condition we fill the collection object and return back to calling method otherwise return null value. Here there is chance of Null pointer exception, when we process the list object without proper null check condition. To prevent Null pointer exception we can return Collections.emptyList()
public static List<String> getCollections(){
return Collections.emptyList();
}
Preventing in Array
When we process the array we should have proper null pointer check, before processing or accessing array value.
if (stringArray[i] != null)
Another way of preventing Null pointer Exception is using try catch block, but it usually increases the lines of code.
Why Null pointer exception something special we always need take care when we do coding because “Error description for NullPointerException doesn’t tells the name of the null variable, which caused the exception”
This Null pointer Exception mostly occurs when we try to search content from a huge collection and reading data from the data base. [Scenario which has more probability of data unavailability]. When we working on different scenario of searching and retrieving data, we must and should concentrate on this null pointer exception otherwise our system will produce incorrect reports and behave unexpectedly.
No comments:
Post a Comment