尝试使用hibernate保存数据时出现NullPointerException

时间:2012-05-03 10:45:32

标签: hibernate

我刚接触hibernate.Now尝试使用hibernate将用户详细信息保存到我的数据库。因为我创建了3 .java文件。

1.首先是HiberUtil.java。代码如下 包hiberpack;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HiberUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}       

2.next是DbMap.java

package hiberpack;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    //import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name = "user_details")
    public class DbMap {


        @Id
        //@GeneratedValue
        @Column(name = "id")
        private long id_auto;

        @Column(name = "name", nullable = false)
        private String username;

        @Column(name = "Smobile", nullable=false)
        private String mobnum;

        public long getId_auto() {
            return id_auto;
        }

        public void setId_auto(long id_auto) {
            this.id_auto = id_auto;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getMobnum() {
            return mobnum;
        }

        public void setMobnum(String mobnum) {
            this.mobnum = mobnum;
        }


    }

3.Below是UserSave servlet

package servlets;
    import hiberpack.DbMap;
    import hiberrepo.Repository;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class UserSave extends HttpServlet {

        private static final long serialVersionUID = 1L;

        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {

            Repository rep = new Repository();
            String name = request.getParameter("uname");
            String mp = request.getParameter("mob");
            DbMap st = new hiberpack.DbMap();
            st.setUsername(name);
            st.setMobnum(mp);
            rep.insertStock(st); 


        }

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            doPost(req, resp);
        }
    }

4.Below是Repository.java

package hiberrepo;

import hiberpack.HiberUtil;
import hiberpack.DbMap;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class Repository {
        SessionFactory sf = HiberUtil.getSessionFactory();
        Session sfSession = null;

        public void insertStock(DbMap storeObj)
        {
            sfSession.save(storeObj);
        }
}

它显示Null指针异常,我试图使用hibernate将数据输入db。错误信息如下

java.lang.NullPointerException
        hiberrepo.Repository.insertStock(Repository.java:15)
        servlets.UserSave.doPost(UserSave.java:25)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

2 个答案:

答案 0 :(得分:1)

致电sfSession时,

Repository.insertStock()为空。你肯定忘了设置会话。 (空指针异常与DbMap storeObj没有任何关系。在输入save方法之前抛出异常。)

答案 1 :(得分:0)

这是您的代码:

Session sfSession = null;
...
sfSession.save(...)

您无法在空引用上调用方法。初始化你的sfSession变量。

PS:Hibernate是一个非常复杂的野兽,需要理解和使用。如果您不知道您无法在空引用上调用方法,并且无法诊断出这样的基本错误,我建议您学习使用Java编程时遇到的问题比使用Web应用程序的问题更简单冬眠。