-2

I'm new to SpringBoot. The problem I am facing with @Autowired annotation. When I'm trying to get the autowired bean, the compiler throw NullPointerException.

This is the service I'm using:

package com.oss.mail.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.handler.LoggingHandler;
import org.springframework.stereotype.Service;

import com.oss.mail.dao.EmailReadingDao;

@Service
public class EmailReadingService {
    Logger logger = LoggerFactory.getLogger(LoggingHandler.class);


    EmailReadingDao emailReadingDao=new EmailReadingDao();

    public void readEmails(){
        logger.info("Called readEmail method from EmailReadingService");
        logger.info("Calling readEmailDao() from EmailReadingDao");
        emailReadingDao.readEmailDao();
    }
}

This is how I defined my DAO:

@Configuration
public class EmailReadingDao {
    Logger logger = LoggerFactory.getLogger(LoggingHandler.class);

    @Autowired
    private Environment env;

    @Autowired
    private GetEmails getEmailsUtil;

  String emailHost;
    String emailPort;
    String emailUserName;
    String emailPassword;
    int NoOfEmails;

    public void readEmailDao(){

        logger.info("Called readEmailDao() from EmailReadingDao");
        Map<String, String> emailsString=new HashMap<String, String>();

        emailHost=env.getProperty("mail.pop3s.host");//Error at thir line.
        emailPort=env.getProperty("mail.pop3s.port");
        emailUserName=env.getProperty("mail.pop3s.username");
        emailPassword=env.getProperty("mail.pop3s.password");
        NoOfEmails=Integer.parseInt(env.getProperty("mail.NoOfEmails"));

And this is what I'm seeing in my logs:

2018-07-30 03:49:38 INFO  o.s.i.handler.LoggingHandler - Called readEmailDao() from EmailReadingDao
Exception in thread "main" java.lang.NullPointerException
    at com.oss.mail.dao.EmailReadingDao.readEmailDao(EmailReadingDao.java:36)
    at com.oss.mail.service.EmailReadingService.readEmails(EmailReadingService.java:20)
    at com.oss.ProductionIncidentAutomation.ProductionIncidentAutomationApplication.main(ProductionIncidentAutomationApplication.java:32)

I'm not sure why the spring is not wiring this class. Please help me in getting the resolution of this.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Abhishek Saxena
  • 1
  • 1
  • 1
  • 3
  • 1
    Autowiering doesn't work if you create an object with `new` keyword. It only works in container managed beans – Jens Jul 30 '18 at 08:08

2 Answers2

1

Autowiring doesn't work if you create an object using the new keyword. It only works in container managed beans. So you have to autowire EmailReadingDao too.

Change:

EmailReadingDao emailReadingDao=new EmailReadingDao();

to:

@Autowired
EmailReadingDao emailReadingDao;

Also EmailReadingDao is not a configuration. You should annotate it with @Repository:

@Repository
public class EmailReadingDao {
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Tried it but not working, I'm not sure why its not working. Error:Field emailReadingDao in com.oss.mail.service.EmailReadingService required a bean of type 'com.oss.mail.dao.EmailReadingDao' that could not be found. Action: Consider defining a bean of type 'com.oss.mail.dao.EmailReadingDao' in your configuration. – Abhishek Saxena Jul 31 '18 at 04:24
  • @AbhishekSaxena Soet correctly. Check this – Jens Jul 31 '18 at 05:38
0

Change EmailReadingDao emailReadingDao=new EmailReadingDao(); to

@Autowired
private EmailReadingDao emailReadingDao;

or even better use constructor injection.

Then also change in your EmailReadingDao from @Configuration to @Component

@Component
public class EmailReadingDao {
...
}
mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52
  • I tried it, but not working. I dont know what I am doing wrong. – Abhishek Saxena Aug 01 '18 at 15:32
  • It worked when I am calling service method by creating REST API . But container fails to autowire beans when I am calling same method from autowired ref in main method of SpringBoot app. Can anyone know the reason? – Abhishek Saxena Aug 03 '18 at 15:30