NullPointerException 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: nullpointerexception java
int x;
x = 10;
Example 4: 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.");
}
}
Example 5: nullpointerexception
public class MyClass{
private Object iAmNowNull;
private Object iAmInitialized=new Object();
public void test(){
System.out.println(iAmInitialized.toString());
System.out.println(iAmNull.toString());
}
Example 6: nullpointerexception
public class MyClass{
private Object iAmNull;
private Object iAmInitialized=new Object();
public void test(){
System.out.println(iAmInitialized.toString());
System.out.println(iAmNull.toString());
}
}