java null pointer exception code example

Example 1: how to use a try catch for a null pointer exception in java

int n;//decalred as null
n = n + 1;//null pointer exception
/////////////////////////////////////
int n = 5;
n = n + 1;//no null pointer exception

Example 2: null pointer exception

NullPointerExceptions are exceptions that occur when you try to 
use a reference that points to no location in memory (null)
 as though it were referencing an object.

Example 3: nullpointerexception java

Integer num;
num = new Integer(10);

Example 4: nullpointerexception java

int x;
x = 10;

Example 5: null pointer exception method

public class MainClass {

    public static void main(String[] args) {
        anotherTest();
        test();
    }
    public static void test() {
        System.out.println("Printed from the test method.");
    }
    public static void anotherTest() {
        System.out.println("Printed from the anotherTest method.");
    }
}