I have a SuperClass.
@Component
public class SuperClass {
private TestBeanSuper testBeanSuper;
public SuperClass(){}
@Autowired
public SuperClass(TestBeanSuper testBeanSuper) {
this.testBeanSuper = testBeanSuper;
}
public void test() {
testBeanSuper.test();
}
}
a SubClass.
@Component
public class SubClass extends SuperClass{
private TestBeanSub testBeanSub;
public SubClass(){}
@Autowired
public SubClass(TestBeanSub testBeanSub) {
this.testBeanSub = testBeanSub;
}
public void test() {
super.test();
}
}
When I call subClass.test(), NullPointerException occurred. The testBeanSuper in SuperClass test method is null.
java.lang.NullPointerException: null
at com.spring.demo.SuperClass.test(SuperClass.java:18) ~[classes/:na]
at com.spring.demo.SubClass.test(SubClass.java:18) ~[classes/:na]
at com.spring.demo.TestController.test(TestController.java:19) ~[classes/:na]
Could someone tell me why this happened?
And when I debugged, I found that testBeanSuper is inside the SubClass instance. The testBeanSuper is defined as private in SuperClass, why it is inside the SubClass instance?