0

I am trying to connect to a mySql database from a servelet, but when i try to do that, i get a null pointer exception:

Connection:

try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost/bmistore", "bmiuser",
                "yMMECajG99yE4YWE");
    } catch (Exception e) {
        System.out.println("No DB found");
        connection = null;
    }
}

Usage:

public List<String> getAllNames() {
    System.out.println("Test");
    List<String> names = new ArrayList<>();

    try {
        Statement statement = connection.createStatement();
        ResultSet results = statement
                .executeQuery("SELECT name FROM patients");

        System.out.println("TEST 2");

        //Add all names to array
        while (results.next()) {
            String name = results.getString("name");
            names.add(name);
        }
    } catch (SQLException e) {
        System.out.println("getAllNames: " + e.getMessage());
    }

    return names;

I have a .sql file in a Folder called RemoteSystemsTempFiles. I'm not sure why it is there, because I'm using an example provided by my lecturer. Thank you for your time.

Exception Stack trace:

May 25, 2014 11:38:09 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Main] in context with path [/BMISystem] threw       exception
java.lang.NullPointerException
at PatientManager.getAllNames(PatientManager.java:42)
at Main.doGet(Main.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.jav        a:305)
at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
5
  • 2
    It would be helpful if you post complete exception stacktrace Commented May 25, 2014 at 1:56
  • Add connection code in getAllNames() method itself and check whether working or not.. Commented May 25, 2014 at 2:13
  • One more observation, parameterize the Arraylist as well. List<String> names = new ArrayList<String>(); Commented May 25, 2014 at 3:29
  • 1
    What is the line PatientManager.getAllNames(PatientManager.java:42)? Commented May 25, 2014 at 3:31
  • also put the code of PatientManager class Commented Jun 4, 2014 at 4:56

4 Answers 4

1

Make sure that PatientManager is not null in that line.

Sign up to request clarification or add additional context in comments.

Comments

1

Do a null check before using PatientManager

if(PatientManager != null){
     PatientManager.getAllNames();
}

Comments

0

I bet connection is null. You are wrapping the connection in a try catch block, but if an exception is thrown then the connection is being set as null.

Later on you use the connection without checking if it is valid: Statement statement = connection.createStatement();

My advice is to check the connection before using it:

public List<String> getAllNames() {
    System.out.println("Test");
    List<String> names = new ArrayList<>();

    if (connection == null) {
         // do something here: return the empty collection or throw an exception.
    }
    try {
        Statement statement = connection.createStatement();
        ResultSet results = statement
                .executeQuery("SELECT name FROM patients");

        System.out.println("TEST 2");

        //Add all names to array
        while (results.next()) {
            String name = results.getString("name");
            names.add(name);
        }
    } catch (SQLException e) {
        System.out.println("getAllNames: " + e.getMessage());
    }

    return names;

Comments

0

The stack trace says at PatientManager.getAllNames(PatientManager.java:42)

That means the PatientManager value is null. Find out how it is getting set as null.

1 Comment

What? No, it doesn't mean that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.