I am getting a null pointer exception while auto wiring one of my classes.
I have tried using different annotations such as @Component
and @ComponentScan
public class ClassA {
public final String str = "String";
public ClassA(){
System.out.println("Class A initiated");
}
}
public class ClassB {
@Autowired
public ClassA classA;
public ClassB(){
System.out.println(classA.str);
}
}
@Configuration
public class Config {
@Bean
public ClassA classA(){
return new ClassA();
}
@Bean
public ClassB classB(){
return new ClassB();
}
}
Stacktrace :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classB' defined in class path resource [com/test/demo/Config.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.test.demo.ClassB]: Factory method 'classB' threw exception; nested exception is java.lang.NullPointerException