Servlet抛出了java.lang.NullPointerException

时间:2013-11-22 11:11:39

标签: java hibernate jsp servlets netbeans-7

我正在使用netbeans 7.4和hibernate 3.6.10开发一个Web应用程序。 我上网并阅读了很多网站,我相信错误是由hibernate和我的servlet之间的错误配置引起的。由于我是jsp,servlet和hibernate的新手,我无法弄清楚我做错了什么。希望你们能解决这个问题。

代码来了。 的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/webasgdvd?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <mapping class="cart.hibernate.PaymentMethod" package="cart.hibernate" resource="cart/hibernate/PaymentMethod.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

PaymentMethod.java

package cart.hibernate;

public class PaymentMethod {
    private int paymentMethodId;
    private String paymentMethod;

    public PaymentMethod(){
    }

    public PaymentMethod(String method){
        this.paymentMethod = method;
    } 

    public int getPaymentMethodId() {
        return paymentMethodId;
    }

    public String getPaymentMethod(){
        return paymentMethod;
    }

    public void setPaymentMethodId(int id) {
        this.paymentMethodId = id;
    }

    public void setPaymentMethod(String method){
        this.paymentMethod = method;
    }  
}

PaymentMethod.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="cart.hibernate.PaymentMethod" table="payment_method">
        <meta attribute="class-description">
            This class contains the payment method detail. 
        </meta>
        <id name="paymentMethodId" type="int" column="payment_method_id">
            <generator class="native"/>
        </id>
        <property name="PaymentMethod" column="payment_method" type="string"/>
    </class>
</hibernate-mapping>

ManagePaymentMethod.java

package cart.hibernate; 

import java.util.List; 
import java.util.Iterator;  
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManagePaymentMethod {

    private static SessionFactory factory;

    public Integer addPaymentMethod(String methodName) { 
        Session session = factory.openSession();  // Error occur here  <--
        Transaction tx = null;
        Integer paymentMethodId = null;

        try {
            tx = session.beginTransaction();
            PaymentMethod payMethod = new PaymentMethod(methodName);
            paymentMethodId = (Integer) session.save(payMethod);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return paymentMethodId;
    }
}

addPaymentMethodServlet.java

package cart.hibernate;

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

public class addPaymentMethodServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String method = request.getParameter("paymentMethod");

        try {
            ManagePaymentMethod manager = new ManagePaymentMethod();

            Integer paymentMethodId = manager.addPaymentMethod(method);
            // Receive from a jsp page and I have checked the value of method is correct

            out.print("..."); // html page

        } finally {
            out.close();
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}

调用servlet时,会捕获异常。以下是错误日志

Nov 22, 2013 6:55:22 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [addPaymentMethodServlet] in context with path [/eShop] threw exception
java.lang.NullPointerException
    at cart.hibernate.ManagePaymentMethod.addPaymentMethod(ManagePaymentMethod.java:93)
    at cart.hibernate.addPaymentMethodServlet.processRequest(addPaymentMethodServlet.java:46)
    at cart.hibernate.addPaymentMethodServlet.doPost(addPaymentMethodServlet.java:90)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

我通过在ManagePaymentMethod.java中执行addPaymentMethod()方法测试了main()addPaymentMethod()正确运行。 此外,从jsp传递到addPaymentMethodServlet.java的值是正确的。

如果您有更多详情,请告诉我。

1 个答案:

答案 0 :(得分:2)

Session session = factory.openSession();

facotory是null

你宣布了

  private static SessionFactory factory;

永远初始化并使用addPaymentMethod方法。