-1

this is my code: my model class:

@Entity
@Table(name="admin")
public class Admin extends Profile{
    public Admin(){}

    public Admin(String mail, String name, String lastName, String password, Date birthDate, int gsm){
        super(mail, name, lastName, password, birthDate, gsm);
    }
}

the DAO class:

@Repository
public class AdminDAO {

    private static final Logger logger = LoggerFactory.getLogger(AdminDAO.class);

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sf){
        this.sessionFactory = sf;
    }
    @SuppressWarnings("unchecked")
    public List<Admin> listAdmins() {
        Session session = this.sessionFactory.getCurrentSession();
        List<Admin> adminsList = session.createQuery("from Admin").list();
        for(Admin a : adminsList){
            logger.info("Admin List::"+a);
        }
        return adminsList;
    }
}

my Service class:

@Service

    public class AdminService {

        @Autowired
        private AdminDAO adminDAO;

        public void setAdminDAO(AdminDAO adminDAO) {
            this.adminDAO = adminDAO;
        }
        @Transactional
        public List<Admin> listAdmins() {
            return this.adminDAO.listAdmins();
        }
    }

when i run my code i get this error message:

java.lang.NullPointerException at com.journaldev.spring.dao.AdminDAO.listAdmins(AdminDAO.java:38)

i added an admin manually in my database, but it still showing the null pointer exception what i am doing wrong ?? note: i have another class that works fine, it gets all entities and when the database is empty, it doesn't generate null pointer exception

  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Erwin Bolwidt Nov 11 '15 at 10:56
  • @ErwinBolwidt no it's not !! please read the question first –  Nov 11 '15 at 10:59
  • 2
    I have read the question and you have clearly not read the duplicate. If you had read it, you would have told us which variable or field was `null` and possibly you would have asked why because you can't find that out. You would also not have posted 300 lines of code, but just the part that was relevant. – Erwin Bolwidt Nov 11 '15 at 11:00
  • ok i will edit my post –  Nov 11 '15 at 11:02
  • You should improvise your setup and config, this thread should help : http://stackoverflow.com/questions/30399338/spring-mvc-creating-a-good-infrastructureconfiguration-and-avoiding-duplicat – We are Borg Nov 11 '15 at 12:16

1 Answers1

0

You missed the @Autowired or the @Inject annotation for the setter method for adminDAO. It has to be

@Autowired
public void setAdminDAO(AdminDAO adminDAO) {
    this.adminDAO = adminDAO;
}

You have to annotate all dependencies of your Bean with @Autowired or @Inject.

gregor
  • 4,733
  • 3
  • 28
  • 43
  • 1
    ok i added '@Autowired' annotation in my AdminService class now the exception is generated in DAO class and not in Service class –  Nov 11 '15 at 11:09
  • You have to add `@Autowired` also to the setter of `sessionFactory`. I've updated the answer. – gregor Nov 11 '15 at 12:12
  • thank you so muuuuch !! what did change after i added these annotations? can u please explain me :) –  Nov 11 '15 at 12:54
  • The `adminDAO` and the `sessionFactory` properties where `null` and hence the runtime throws a `NullPointerException` when you try to access this (for example in the `listAdmins` method). `@Autowire` or `@Inject` tells the spring container to inject a apropriate value if known and voila the propertie isn't `null` anymore. For more details have a lookl at the [spring docs](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html). – gregor Nov 12 '15 at 13:09