Check out example codes for "exception handling in java null pointer". It will help you in understanding the concepts better.
Code Example 1
n=10;
Code Example 2
/**
* @param obj An optional foo for ____. May be null, in which case
* the result will be ____.
*/
public void doSomething(SomeObject obj) {
if(obj == null) {
//do something
} else {
//do something else
}
}
Code Example 3
// A Java program to demonstrate that invoking a method
// on null causes NullPointerException
import java.io.*;
class GFG
{
public static void main (String[] args)
{
// Initializing String variable with null value
String ptr = null;
// Checking if ptr.equals null or works fine.
try
{
// This line of code throws NullPointerException
// because ptr is null
if (ptr.equals("gfg"))
System.out.print("Same");
else
System.out.print("Not Same");
}
catch(NullPointerException e)
{
System.out.print("NullPointerException Caught");
}
}
}
Learn ReactJs, React Native from akashmittal.com