Why NullPointerException?
I have a abstract class and a derived class. Look at provided code:-
public abstract class Parent{
public Parent(){
init();
}
public abstract void init();
}
public class Child extends Parent{
private String mTitle = null;
public Child(){
super();
System.out.println(mTitle.toString());
}
public void init(){
mTitle = "It' a test";
}
}
When I will execute the above code it will throw NullPointerException on printing the value of mTitle. If you check the code in constructor of parent I have called the the abstract method which will called the init method of derived class, In abstract method I have initialize the value of mTitle value as ="It's a test";
After calling parent constructor derived class have to call the System.out.println.
If it is doing in that way then why it is throwing NullPointerException.
But, If I just leave the assignment of mTitle it will not throw Exception like:-
private String mTitle;
If initialization of variable occur on calling of the contruct of class and we know by default global object have initialize to null. But in this case it will not throw Exception.