GeekySelene
GeekySelene

Reputation: 927

Spring `@Autowire` field is `null` eventhough it works fine in other classes

Spring @Autowire field is null even though it works fine in other classes successfully.

public class SendRunner implements Runnable {

private String senderAddress;
@Autowired
private SubscriberService subscriberService;

    public SendRunner(String senderAddress) {
    this.senderAddress = senderAddress;
}

@Override
public void run() {
    sendRequest();
}

  private void sendRequest() {
    try {
        HashMap<String, String> dataMap = new HashMap<>();
        dataMap.put("subscriberId", senderAddress);
         HttpEntity<?> entity = new HttpEntity<Object>(dataMap, httpHeaders);
         Subscriber subscriber = subscriberService.getSubscriberByMsisdn(senderAddress);
        } catch (Exception e) {
          logger.error("Error occurred while trying to send api request", e);
    }
}

Also this class is managed as a bean in the dispatcher servlet :

    <bean id="SendRunner" class="sms.dating.messenger.connector.SendRunner">
    </bean>

In here i'm getting a null pointer exception for subscriberService. What would be the possible reason for this? Thanks in advance.

Upvotes: 0

Views: 133

Answers (3)

Mohit Sharma
Mohit Sharma

Reputation: 380

Your bean not in Spring Managed context, below can be the reasons.

  1. Package sms.dating.messenger.connector not in Component scan.

  2. You are moving out of the Spring context by creating an object with new (see below), this way you will not get the autowired fields.

    SendRunner  sendRunner = new SendRunner () ,
    sendRunner.sendRequest();
    

Just check how I implement. Hope this will help.

@RestController
public class RestRequest {

@Autowired
SendRunner sendRunner;

@RequestMapping("/api")
public void Uri() {
    sendRunner.start();
   }
}

SendRunner class

@Service
public class SendRunner extends Thread{ 

@Autowired
private SubscriberService subscriberService;

@Override
public void run() {
    SendRequest();

}

private void SendRequest() {
    System.out.println("Object is " + subscriberService);
    String senderAddress = "address";
    subscriberService.getSubscriberByMsisdn(senderAddress);
}
}

Below are the logs printed when I hit the REST api.

Object is com.example.demo.SubscriberService@40f33492

Upvotes: 0

Rajesh Katadi
Rajesh Katadi

Reputation: 144

Can you please try with below code snippet

@Configuration
                public class Someclass{

                     @Autowired
                     private SubscriberService subscriberService;

                     Thread subscriberThread = new Thread() {
                                @Override
                                public void run() {
                                  try {
                        HashMap<String, String> dataMap = new HashMap<>();
                        dataMap.put("subscriberId", senderAddress);
                         HttpEntity<?> entity = new HttpEntity<Object>(dataMap, httpHeaders);
                         Subscriber subscriber = subscriberService.getSubscriberByMsisdn(senderAddress);
                        } catch (Exception e) {
                          logger.error("Error occurred while trying to send api request", e);
                    }

                                }
                            };

                }

Upvotes: 1

Rajesh Katadi
Rajesh Katadi

Reputation: 144

Can you please annotate your SendRunner class with @Component or @Service and include the SendRunner package in componentscanpackage

Upvotes: 0

Related Questions