0
Exception in thread "main" java.lang.NullPointerException
    at com.hibernate.UserDAO.findAll(UserDAO.java:154)
    at test.main(test.java:12)

I am getting this error when trying to do anything with any of my DAOs any ideas? Thanks in advance!

Here's my Data source in my Spring applicationContext.xml file.

<bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver">
        </property>
        <property name="url"
            value="jdbc:mysql://cs180admin.db.5875115.hostedresource.com:3306/cs180admin">
        </property>
        <property name="username" value="xxxxx"></property>
        <property name="password" value="xxxxx"></property>
    </bean>

Myeclipse connects to the database fine because I am able to reverse engineer it.

Test.java that I am using to test that throws error.

import com.hibernate.UserDAO;


    public class test {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            UserDAO dao = new UserDAO();
            System.out.println(dao.findAll());


        }

    }

Edit:

UserDAO.java
    public List findAll() {
        log.debug("finding all User instances");
        try {
            String queryString = "from User";
        Line 154 -->    return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

The Line isn't commented out, I just did the arrow to show where the error is happening.

1 Answer 1

2

Is it possible to see the code of your UserDAO class? The NPE is triggered inside that class at the line 154 so the problem is there

Sign up to request clarification or add additional context in comments.

3 Comments

are you calling the DAO directly by creating the object? As far as I know you have to create a bean for your DAO in servlet.xml. If you're not doing this getHibernateTemplate() will return null. This is the only reason I know that can trigger this exception, otherwise, if getHibernateTemplate() is not returning null you should post the whole stacktrace.
let me know if that was the problem, in case it works I will really appreciate if you vote up my answer! Thanks
As @thermz said, you have to create the DAO bean not by yourself, but in the applicationContext.xml file, and inject the hibernateTemplate property and add this property in your UserDAO class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.