0

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)

Turing85
  • 18,217
  • 7
  • 33
  • 58
joy
  • 47
  • 1
  • 5
  • You need to start the Spring application so it injects your dependencies, something like `SpringApplication.run(Application.class, args);` – m0skit0 Jul 31 '19 at 18:59
  • In order for Spring to wire-up your bean, you need to let Spring initialize the bean. This can be done either [programmatically](https://stackoverflow.com/questions/25775888/retrieve-bean-programmatically) or by wiring your `DependencyInjectionExample ` into another bean. – Turing85 Jul 31 '19 at 18:59
  • I tried that already, still same issue. I even tried @Controller annotation. – joy Jul 31 '19 at 19:00
  • Also ensure the Spring application can component scan the packages. – Compass Jul 31 '19 at 19:00
  • @joy if you did, you did it wrong. You cannot instantiate instances yourself (i.e. by using `new`) if you want that Spring wires up the bean for you. – Turing85 Jul 31 '19 at 19:01

1 Answers1

2

First of all you not call application context which is responsible to register beans:

SpringApplication.run(MainClass.class, args);

Secondly, you create DependencyInjectionExample by hand.

It will never work in this way. If you want to do something similar you need to code it that way:

public static void main(String[] args) {
    ConfigurableApplicationContext context = SpringApplication.run(MainClass.class, args);
    DependencyInjectionExample dependencyexample  = context.getBean(DependencyInjectionExample.class);
    System.out.println(dependencyexample.printHigh()); 
}
mslowiak
  • 1,688
  • 12
  • 25
  • thanks mslowiak, above code worked. So basically, I was messing up with the object creation in the spring framework. We should not create objects using new keyword in Spring if the class is marked as 'component'. Because when we mark it as 'component' it will create a bean which eventually will give us only one instance. Correct me if I am wrong ! – joy Jul 31 '19 at 19:18
  • @joy Yes. It is like this. – mslowiak Jul 31 '19 at 20:51