Biboo Chung
Biboo Chung

Reputation: 121

Servlet threw java.lang.NullPointerException

I am using netbeans 7.4 with hibernate 3.6.10 to develop a web application. I have surfed the net and read quite a number of site, I believe the error is caused by wrong configuration between hibernate and my servlet. As I am new to jsp, servlet and hibernate, I cannot figure out what have I did wrongly. Hope you guys can solve this problem.

Here comes the code. 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";
    }

}

When the servlet is called, exception is caught. The following is the error log

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)

I have tested the addPaymentMethod() by executing a main() method in the ManagePaymentMethod.java. The addPaymentMethod() run correctly. Moreover, the value that passed from jsp to addPaymentMethodServlet.java is correct.

If you have more details, please let me know.

Upvotes: 2

Views: 9676

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 122026

in the line

Session session = factory.openSession();

facotory is null

You declared

  private static SessionFactory factory;

And never initialized and using in addPaymentMethod method.

Upvotes: 2

Related Questions