Please see following code, I am trying to learn @Autowire attribute so tried following example. But I am getting a null pointer exception.
@Component
class GoodNightService {
public String SayGoodNight() {
return "Good night";
}
}
public class DependencyInjectionExample {
@Autowired
GoodNightService goodnightservice;
public String printHigh()
{
return goodnightservice.SayGoodNight();
}
}
You can see I have created a component and so that beans can be created of it. I am trying to make use of dependency injection principle using @Autowired attribute. So created a 'DependencyInjectionExample' class and autowired the service.
I created following main function to get the object of the example class.
public static void main(String[] args) {
DependencyInjectionExample dependencyexample = new DependencyInjectionExample();
System.out.println(dependencyexample.printHigh());
}
But I am getting nullpointer exception:
Exception in thread "main" java.lang.NullPointerException at com.example.first.mySimpleSpringApp.DependencyInjectionExample.printHigh(DependencyInjectionExample.java:13) at com.example.first.mySimpleSpringApp.GoodNightServiceImpl.main(GoodNightServiceImpl.java:9)