1

While establishing connection with mysql server getting a java.lang.NullPointerException. I have written a method getConn() to establish connection in EmployeeModel.java

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import java.sql.Connection;

import dto.Employee;

public class EmployeeModel {
public Connection getConn() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
    Class.forName("com.mysql.jdbc.Driver"); //Exception occuring at this line
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/todo","root","root");
    return con;
}
public String saveEmployee(Employee emp) throws Exception {
    String result="fail";
    Connection con = null;
    try {

        con=getConn(); //method call 
        PreparedStatement pstmt=con.prepareStatement("insert into employee_registration values(0,?,?,?,?,?,?)");
        pstmt.setString(1, emp.getName());
        pstmt.setString(2, emp.getDepartment());
        pstmt.setString(3, emp.getUsername());
        pstmt.setString(4, emp.getPassword());
        pstmt.setString(5, emp.getContact());
        pstmt.setString(6, emp.getDob());

        int count=pstmt.executeUpdate();
        if(count>0) {
             result="success";
        }

    }finally {
        con.close();
    }
    return  result;
}
}

when I am calling getConn() from saveEmployee(Employee emp) method of same class getting a following exception.

java.lang.NullPointerException
at model.EmployeeModel.saveEmployee(EmployeeModel.java:42)
at controller.EmpRegistrationController.service(EmpRegistrationController.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:609)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

While I am calling the getConn() method from test cases(JUnit) in EmployeeTest.java, connection is getting established successfully.

public class EmployeeTest {

@Test
public void testForGetConnectionPositive() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
    EmployeeModel eModel=new EmployeeModel();
    Connection con=eModel.getConn();// connection is getting established successfully 
    assertNotNull(con);
}
@Test
public void testForEmployeetWithValidValue() throws Exception {
    EmployeeModel eModel=new EmployeeModel();
    Employee emp=new Employee();

    emp.setName("opq");
    emp.setDepartment("computer");
    emp.setUsername("dummyName");
    emp.setPassword("pass2924");
    emp.setContact("7878787878");
    emp.setDob("2/5/2019");

    String actualValue=eModel.saveEmployee(emp);
    assertEquals("success", actualValue);
}

}

My question why it is giving error while calling getConn() method from application.

  • Read the stack trace. The exception does not happen when you claim it does. It happens in saveEmployee, at line 42. We don't know which line that is, but I would guess it's `pstmt.setString(1, emp.getName());`, and you get the exception because `emp` is null. – JB Nizet Jan 24 '20 at 18:26

0 Answers0