java.lang.NullPointerException error

1,157 views
Skip to first unread message

nick....@gmail.com

unread,
May 9, 2013, 4:47:59 AM5/9/13
to java-gen...@googlegroups.com
Hi,

I am trying to get hibernate-generic-dao to work using the generic DAO examples at http://code.google.com/p/hibernate-generic-dao/wiki/GenericDAOExamples. I placed the 1.2 version of dao.jar and search.jar in my project. I am using Hibernate 4.2 and I am not using Spring.

I created a WebsiteDAO.java file, like so:

===================================

import java.util.List;

import com.googlecode.genericdao.dao.jpa.GenericDAO;
import com.pasionat.models.Website;

public interface WebsiteDAO extends GenericDAO<Website, Long> {
    public List<Website> findByWebsiteType(String websiteType);
}

===================================


I created a WebsiteDAOMySQL.java file, like so:

============
import com.googlecode.genericdao.dao.jpa.GenericDAOImpl;
import com.googlecode.genericdao.search.Search;
import com.pasionat.models.Website;

public class WebsiteDAOMySQL extends GenericDAOImpl<Website, Long> implements WebsiteDAO {
   
    public List<Website> findByWebsiteType(String websiteType)
    {
        return search(new Search().addFilterEqual("websiteType", websiteType));
    }
}
======

Now when I call:

List<Website> websites = null;
websites = websiteDao.findAll();

I get:

Exception in thread "main" java.lang.NullPointerException
    at com.googlecode.genericdao.dao.jpa.JPABaseDAO.getMetadataUtil(JPABaseDAO.java:82)
    at com.googlecode.genericdao.dao.jpa.JPABaseDAO._all(JPABaseDAO.java:206)
    at com.googlecode.genericdao.dao.jpa.GenericDAOImpl.findAll(GenericDAOImpl.java:61)
    at com.pasionat.fetch.Main.main(Main.java:136)


And when I call:

List<Website> websites = null;
websites = websiteDao.findByWebsiteType("feed");

I get:

Exception in thread "main" java.lang.NullPointerException
    at com.googlecode.genericdao.dao.jpa.JPABaseDAO._search(JPABaseDAO.java:322)
    at com.googlecode.genericdao.dao.jpa.GenericDAOImpl.search(GenericDAOImpl.java:123)
    at com.pasionat.dao.WebsiteDAOMySQL.findByWebsiteType(WebsiteDAOMySQL.java:145)
    at com.pasionat.fetch.Main.main(Main.java:136)

I probably forgot something. But I can not figure out what. Could you please give me some pointers? Thanks.

Kind regards,

Nick

nick....@gmail.com

unread,
May 10, 2013, 6:24:39 AM5/10/13
to java-gen...@googlegroups.com, nick....@gmail.com
Hi,

I think I got a step closer to the solution. I realized I also had to download and install dao-hibernate-1.2.0.jar and search-hibernate-1.2.0.jar and replaced import com.googlecode.genericdao.dao.jpa with import com.googlecode.genericdao.dao.hibernate in my code. This way I am using the already installed Hibernate without JPA. I am still getting nullpointerexception errors.

Now when I run:


List<Website> websites = null;
websites = websiteDao.findAll();

I get:

Exception in thread "main" java.lang.NullPointerException
    at com.googlecode.genericdao.dao.hibernate.HibernateBaseDAO._all(HibernateBaseDAO.java:418)
    at com.googlecode.genericdao.dao.hibernate.GenericDAOImpl.findAll(GenericDAOImpl.java:61)
    at com.pasionat.fetch.Main.main(Main.java:137)

when I run:


List<Website> websites = null;
websites = websiteDao..findByWebsiteType("feed");


I get:

Exception in thread "main" java.lang.NullPointerException
    at com.googlecode.genericdao.dao.hibernate.HibernateBaseDAO.getSession(HibernateBaseDAO.java:68)
    at com.googlecode.genericdao.dao.hibernate.HibernateBaseDAO._search(HibernateBaseDAO.java:494)
    at com.googlecode.genericdao.dao.hibernate.GenericDAOImpl.search(GenericDAOImpl.java:111)
    at com.pasionat.dao.WebsiteDAOMySQL.findByWebsiteType(WebsiteDAOMySQL.java:145)
    at com.pasionat.fetch.Main.main(Main.java:137)

So I guess there is something wrong with the Hibernate session, there is none. So I tried following the samples, but unfortunately they all use Spring. I added a BaseDAO class with the public void setSessionFactory(SessionFactory sessionFactory) . That didn't work, as it doesn't get run. So I tried adding Spring to the project and copied all the xml files from the samples, again the function in the BaseDAO class doesn't get executed, so I guess no Hibernate session is created. My question is, how can I create/get a Hibernate session preferably without using Spring? Thanks.

Kind regards,

Nick



Op donderdag 9 mei 2013 10:47:59 UTC+2 schreef nick....@gmail.com het volgende:

herlani...@gmail.com

unread,
May 10, 2013, 10:30:25 AM5/10/13
to java-gen...@googlegroups.com, nick....@gmail.com
Did you initialize your session factory ?

nick....@gmail.com

unread,
May 10, 2013, 10:47:40 AM5/10/13
to java-gen...@googlegroups.com, nick....@gmail.com, herlani...@gmail.com
Thanks for the reply. Honestly I do not know I initialized the sessionfactory. I think I have. I have been playing around a bit more and I know have the following:


public class WebsiteDAOMySQL extends GenericDAOImpl<Website, Long> implements WebsiteDAO {

    public WebsiteDAOMySQL(final SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);

    }
   
    public List<Website> findByWebsiteType(String websiteType)
    {
        return search(new Search().addFilterEqual("websiteType", websiteType));
    }
}

This seems to set the session factory. Does it also initialize the session factory? But know I get:

Exception in thread "main" org.hibernate.HibernateException: createQuery is not valid without active transaction
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348)
    at com.sun.proxy.$Proxy4.createQuery(Unknown Source)
    at com.googlecode.genericdao.search.hibernate.HibernateSearchProcessor.search(HibernateSearchProcessor.java:85)

    at com.googlecode.genericdao.dao.hibernate.HibernateBaseDAO._search(HibernateBaseDAO.java:494)
    at com.googlecode.genericdao.dao.hibernate.GenericDAOImpl.search(GenericDAOImpl.java:111)
    at com.pasionat.dao.WebsiteDAOMySQL.findByWebsiteType(WebsiteDAOMySQL.java:154)
    at com.pasionat.fetch.Main.main(Main.java:141)

How do I get an active transaction? Thanks.

Kind regards,

Nick

HERLANI JUNIOR

unread,
May 10, 2013, 11:09:31 AM5/10/13
to nick....@gmail.com, java-gen...@googlegroups.com
Are you using spring ? I could get that you are calling the methods from a Main class that is outside a transaction. Try make the call from a servlet (that is natively inside a transaction) !

To bind the "session factory" you must initialize hibernate before calling the DAO method !

nick....@gmail.com

unread,
May 10, 2013, 2:23:00 PM5/10/13
to java-gen...@googlegroups.com, nick....@gmail.com
Thanks for the reply. Your comment that I had to initialize Hibernate before calling the DAO method really helped. I managed to get it to work, probably in a very hackerish, non Java compliant way. So if you have tips to improve it or warnings why what I am doing is dangerous, please let me know. I am not using Spring. I am also not able to make the call from a servlet, since this isn't a web based project. I did the following:

WebsiteDAO.java
=============

public interface WebsiteDAO extends GenericDAO<Website, Long> {
    public List<Website> findByWebsiteType(String websiteType);
}

WebsiteDAOMySQL.java
==================
public class WebsiteDAOMySQL extends GenericDAOImpl<Website, Long> implements WebsiteDAO, BaseDAO {
   
    public WebsiteDAOMySQL() {

        super.setSessionFactory(sessionFactory);
    }
   
    public List<Website> findByWebsiteType(String websiteType)
    {
        return search(new Search().addFilterEqual("websiteType", websiteType));
    }
}

BaseDAO.java
==========
public interface BaseDAO
{
    final public SessionFactory sessionFactory = new Configuration()
        .configure() // configures settings from hibernate.cfg.xml
        .buildSessionFactory();
    final public Session session = sessionFactory.getCurrentSession();
    final public Transaction tx = session.beginTransaction();
}

Kind regards,

Nick
Reply all
Reply to author
Forward
Message has been deleted
0 new messages