0

我编写了一个简单的 Web 应用程序来注册用户。但我收到如下错误。

INFO: Configuration resource: /com/vaannila/config/adminuser.hbm.xml
java.lang.NullPointerException
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1399)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1378)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1298)
    at com.vaannila.utility.FactoryUtil.<clinit>(FactoryUtil.java:18)
    at com.vaannila.service.AdminUserBO.addUser(AdminUserBO.java:17)
    at com.vaannila.delegate.AdminUserBusinessDelegate.addUser(AdminUserBusinessDelegate.java:11)
    at com.vaannila.action.AdminUserAction.addUser(AdminUserAction.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
    at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
    at com.vaannila.service.AdminUserBO.addUser(AdminUserBO.java:17)
    at com.vaannila.delegate.AdminUserBusinessDelegate.addUser(AdminUserBusinessDelegate.java:11)
    at com.vaannila.action.AdminUserAction.addUser(AdminUserAction.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
    at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
1123damuammudamodardamuchinni@gmail.com

下面还有 FactoryUtil 类和 BO 类的代码

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FactoryUtil {
    //Static Session Factory
        private static org.hibernate.SessionFactory sessionFactory;

        private FactoryUtil() {}

        static {

            //Creates the SessionFactory based on the XML Configuration
            try{
            Configuration configs = new Configuration();
            sessionFactory = configs.configure("/com/vaannila/config/adminuser.hbm.xml").buildSessionFactory();
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        public static SessionFactory getInstance() {
            return sessionFactory;
        }

        public Session openSession() {
            return sessionFactory.openSession();
        }

        public Session getCurrentSession() {
            return sessionFactory.getCurrentSession();
        }

        public static void close() {
            if (sessionFactory != null)
                sessionFactory.close();
            sessionFactory = null;
        }

        public static FactoryUtil getSessionFactory() {
            // TODO Auto-generated method stub
            return null;
        }

}

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.vaannila.domain.AdminUserVO;
import com.vaannila.utility.FactoryUtil;

public class AdminUserBO {



    public void addUser(AdminUserVO auserVO) {
        // TODO Auto-generated method stub

        Session session = FactoryUtil.getInstance().openSession();
        Transaction transaction = null;
        Long singerId = null;

        try {
            transaction = session.beginTransaction();
            AdminUserVO userVO = new AdminUserVO();
            singerId = (Long)session.save(userVO);
            transaction.commit();
        }
        catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }


    }

}

下面是映射文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.vaannila.domain.AdminUserVO" table="users">
<id name="userId" type="int" column="userId" >
<generator class="increment"/>
</id>
<property name="userName">
<column name="userNm" />
</property>
<property name="password">
<column name="password" />
</property>
<property name="emailAddress">
<column name="emailId" />
</property>

</class>
</hibernate-mapping>

请任何人都可以建议我解决上述问题

编辑

public class AdminUserVO implements java.io.Serializable{
    private String userName;
    private String userId;
    private String password;
    private String emailAddress;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmailAddress() {
        return emailAddress;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

}


User Name  damodar
User Id  damuammu
Password  1123
Email Address  damuchinni@gmail.com

以上是我输入的数据,同样也打印在 stacttrace 中。

4

1 回答 1

2

Hibernate 配置方法

public Configuration configure(String resource)
                        throws HibernateException

应该采用您的hibernate.cfg.xml文件的路径,而不是映射文件。请参阅http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/cfg/Configuration.html#configure%28java.lang.String%29

于 2012-08-19T13:37:57.817 回答