NullPointerException java api code example

Example 1: NullPointerException

TestObject a = new TestObject();String b = a.getTestResult();b.toString();

Example 2: nullpointerexception java

Integer num;
num = new Integer(10);

Example 3: nullpointer extension

public class C {
private B b = null;
private D d = null;
private E e = null;

public C(B b, E e, D d)
{
    this.b = b;
    this.e = e;
    this.d = d;
}

Example 4: nullpointerexception java

int x;
x = 10;

Example 5: nullpointerexception

public class MyClass{
	private Object iAmNull;//This is null by default
    private Object iAmInitialized=new Object();
    public void test(){
    	System.out.println(iAmInitialized.toString());//prints that object
        System.out.println(iAmNull.toString());//throws a NullPointerException
    }
}

Tags:

Java Example