Any specific reason why i am getting nullpointer exception at line 27 and 28 respectively

167 Views Asked by At

Null pointer exception at object creation places creating a c object here and passing it to registration method getting null pointer exception

I have commented the two lines where null pointer exception arises.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bookstore.formbeans.RegistrationActionForm;
import com.bookstore.pojo.Customer;
import com.bookstore.service.CustomerService;

public class RegistrationAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String status ="";
        RegistrationActionForm actionForm= (RegistrationActionForm) form;
        Customer c = new Customer();
        c.setCno(actionForm.getCno());
        c.setCname(actionForm.getCname());
        c.setCphon(actionForm.getCphon());
        c.setCaddr(actionForm.getCaddr());
        
        CustomerService cService = new CustomerService();
        //status = cService.registration(c);
        return mapping.findForward(status);
    }
}
import com.bookstore.pojo.Customer;
import com.bookstore.util.HibernateUtil;

public class CustomerService {
    public String registration(Customer c){
        String status="";
        Transaction tx = null;
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            Session session = sessionFactory.openSession();
            Customer c1 = (Customer)session.get(Customer.class, c.getCno());
            if(c1 == null){
                tx = session.beginTransaction();
                session.save(c);
                tx.commit();
                status="success";
            }else{
                status = "existed";
            }
        } catch (Exception e) {
            //tx.rollback();
            status = "failure";
            e.printStackTrace();
        }
        return status;
    }
}

tried instantiating with different methods

1

There are 1 best solutions below

0
rzwitserloot On

First commented line: Cannot possibly itself be the cause of an NPE. The actual NPE is in the registration method.

Second commented line: Because tx is null. Which it will be if an exception occurs. For example in getSessionFactory().

Limit your try statements. Here your exception clearly assumes that session.save(c) will fail, and that there is no other explanation. But that's clearly false: There are way more lines in that try block, and they could all be failing. If you are going to assume a particular line or error occured in a generalized exception block like this, the try block should contain just that.

In addition, when an exception occurs inside an exception handler, the exception that kicked it all off is just gone. So, you now can't tell which actual line got you to the exception handler. Fix that - for example by remarking out tx.rollback() temporarily to see what happens, and you know more. To fix this code properly, that try/catch should wrap session.save(c); tx.commit() and nothing else.

Also note that tx.rollback() can itself throw too.

In general this model of exception handling (catch it, take most of the info and toss it in the bin, print a stack trace to System.err which is not how you do logging, set a very generc 'failure' status, and then carry on as if nothing went wrong) is just broken. If an exception occurs, embrace it. Your registration method should be throwing something, not swallowing exceptions. In general if it does hibernate things, throwing SQLException is fine. Better than fine, you should be doing that.