Jack cole
Jack cole

Reputation: 447

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

Upvotes: 1

Views: 1966

Answers (4)

I had the same problem, in the same line, but I solved it by adding to the project the JAR for the MySQL connection.

Before, it was not included in the project because all the data for the connection had been added within the server configuration (Payara Server 5), just for this reason the error does not occur when displaying the project on the server. However, when running it in JUnit this is not taken into account, so it is necessary to add the JAR to make the connection directly from the project and not from the server.

I hope this is useful, for those who have the same problem.

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

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.

Upvotes: 2

JamesB
JamesB

Reputation: 7894

The MySQL jar is missing from the classpath of your server's container whereas it is present when you run the standalone main application.

Upvotes: 1

evg
evg

Reputation: 1338

Make sure mysql driver jar is in your lib folder (or classpath if its console app). Try to specify port to mysql (3306 if you didn't change it).

Upvotes: 1

Related Questions