0

I am getting userId from database using HQL in my DAO class.But when itried to set it in session , it is giving null pointer exception . here is the code in DAO class.

@Repository("loginDAO")
public class LoginDAOImpl implements LoginDAO{

    @Resource(name="sessionFactory")
    protected SessionFactory sessionFactory;
    HttpServletRequest request;
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
public Session getSession(){
    return sessionFactory.openSession();
}
    public boolean checkLogin(String userName, String userPassword) {

        Session session=sessionFactory.openSession();
        boolean userExist=false;
        String sql_query="from Users as o where o.userName=? and o.userPassword=?" ;
        Query query=session.createQuery(sql_query);
        query.setParameter(0, userName);
        query.setParameter(1, userPassword);
        List list=query.list();

        if((list.size()>0) && (list!=null)){
            userExist=true;
            //TO get the user id to insert into orders table 
            Users user=null;
            user=(Users) query.list().get(0);
            Long id=user.getId();
            System.out.println("Id is "+ id);
            **//This is giving Null Pointer
            request.getSession().setAttribute("id", id);**


            }
        session.close();
    if(userExist==false){System.out.println("user name or password wrong");}
        return userExist;
    }

}

i am sure that id has value. Thanks in advance.

jagga
  • 135
  • 5
  • 15

0 Answers0