NULL Autowired object in Main method while using it in Timer class object

Akshay Busa :

I am running springboot application with TimerTask my object of service is showing null.

I have tried various methods but unable to get rid of the Null pointer exception.

main class .

@SpringBootApplication
@ComponentScan(basePackages = {"com.comments.demo"}) 
public class NotifyMain {

    @Autowired
    static
    NotifyService notifyService;


    public static void main(String[] args) {

        Timer timer1 = new Timer();
        timer1.schedule(notifyService, 10, 10);
        SpringApplication.run(NotifyMain.class, args);

    }
}

Service class

package com.comments.demo;
@Service
@Configurable
public class NotifyService extends TimerTask{

    @Autowired  
    ListNotification listElement;
        @Override
    public void run() {
    Notification notification= new Notification();
        listElement.add(notification);
    }

ListNotification and Notification class are working fine.

console

Exception in thread "main" java.lang.NullPointerException
at java.util.Timer.sched(Timer.java:399)
at java.util.Timer.schedule(Timer.java:248)
at com.comments.demo.NotifyMain.main(NotifyMain.java:22)

here is the code of ListNotification

 package com.comments.demo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ListNotification {

    private List<Notification> notifications = new ArrayList<>();

     @Autowired
     private NotificationObserver notificationObserver;

    public void setNotifications(List<Notification> notifications) {
        this.notifications = notifications;
    }

    public void add(Notification notification) {
        notifications.add(notification);
        notifyListeners(notification); 
    } 

    private void notifyListeners(Notification newValue) {
        notificationObserver.observation(newValue);
    }
}

first I was getting listElement object null. so i got that instead of using the new NotifyService() in parameter of schedule method i should use the injected bean but how to do it I don't know.

Faraz :

You cannot autowire or manually wire static fields in Spring. Instead, try setter injection:

private static NotifyService notifyService;

@Autowired
public void setNotifyService(NotifyService notifyService){
    NotifyMain.notifyService= notifyService;
}

But still, there won't be any guarantee if NotifyService is injected before used. You can also try 2nd approach:

private static NotifyService notifyService;

@Autowired
private NotifyService autowiredNotifyService; //same as above but non-static this time. And you autowire this one.

@PostConstruct
private void init() {
   NotifyMain.notifyService = this.autowiredNotifyService;
}

3rd Approach -> Use constructor injection:

private static NotifyService notifyService;

@Autowired
public NotifyMain(NotifyService notifyService){
    NotifyMain.notifyService= notifyService;
}

Do know that autowiring to static field is undesirable. One should NOT do it.

Since your application is more like a console based application, this approach can also be taken:

@SpringBootApplication
public class NotifyMain implements CommandLineRunner {

@Autowired
private NotifyService notifyService;

public static void main(String[] args) {
    SpringApplication.run(NotifyMain.class, args);
}

@Override
public void run(String... args) {
    Timer timer1 = new Timer();
    timer1.schedule(notifyService, 10, 10);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=113094&siteId=1