Search code examples
javahtmljspweb-applicationsservlets

NullPointerException is thrown when trying to call JDBC from a servlet


I'm running my servlet RegistrationServlet , and I try to create a Database object

Here is the servlet :

@WebServlet("/register")
public class RegistrationServlet extends HttpServlet 
{
    private static final long serialVersionUID = 1L;

    @Override

    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
    {
        // create a new connection to mysql database  , with this we put the new client in the database 

        Database myDabatase = null;

        try {
            myDabatase = new Database();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }  // create a new database 

        myDabatase.createDatabaseAndTables();  // create the tables of the database


        HttpSession session = request.getSession();
        synchronized(session) 
        {


              boolean returnValue = myDabatase.addNewClient("david", "cole", "jamie", "123456789", "johnny", "blabla");     

          if (returnValue == true)  // client was added 
          {
              String addressPath = "/WEB-INF/results/show-name.jsp";
              RequestDispatcher dispatcher = request.getRequestDispatcher(addressPath);
              dispatcher.forward(request, response);
          }

          else if (returnValue == false)  // client was not added becuase he's already registered 
          {

          }


        }
    }

}

Here is the complete class :

But when I execute that line in my servlet :

boolean returnValue = myDabatase.addNewClient("david", "cole", "jamie", "123456789", "johnny", "blabla");

I get a NullPointerException that says :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at db.Database.<init>(Database.java:18)
    at servlets.RegistrationServlet.doGet(RegistrationServlet.java:28)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Which led me to the line :

Class.forName("com.mysql.jdbc.Driver");

in the constructor of the Database class , and as you can see in the track trace :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

But , when I execute the same program as a Main program :

public class Main {

    public static void main(String [ ] args) throws Exception
    {

        Database myConnection = new Database();
        myConnection.createDatabaseAndTables();
        boolean returnValue = myConnection.addNewClient("david", "cole", "jamie", "123456789", "johnny", "blabla");

    }

}

Everything is okay , and I get no NullPointerException .

Then what's wrong if I do that same thing from a servelt ? why the NullPointerException ?

Regards


Solution

  • The MySQL driver is missing from the classpath when you run the servlet. The JAR file with the driver probably needs to be added to the WEB-INF/lib directory of your webapp.