Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8359817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:16:19+00:00 2026-06-09T11:16:19+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:16:22+00:00Added an answer on June 9, 2026 at 11:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.