Hello I have the following code that gives me a NullPointerException
when I call
the function child.any()
Why this happens and how to solve this?
public main {
public void main(String[] args) {
Parent parent = new Parent();
Child child = new Child();
Tmp tmp = new Tmp();
parent.setter(tmp);
child.any();
}
}
public Parent {
Tmp tmp;
protected void setter (Tmp tmp) {
this.tmp = tmp;
}
}
public Child extends Parent {
protected void any() {
tmp.printTmp();
}
}
public Tmp {
public void printTmp() {
System.out.println("Hello");
}
}