user3011902
user3011902

Reputation:

NullPointer exception when connecting to a mySQL database in Java EE

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)

Upvotes: 0

Views: 444

Answers (4)

user3705595
user3705595

Reputation:

Do a null check before using PatientManager

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

Upvotes: 1

Alexandre Santos
Alexandre Santos

Reputation: 8338

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;

Upvotes: 0

sofs1
sofs1

Reputation: 4176

Make sure that PatientManager is not null in that line.

Upvotes: 1

Siva
Siva

Reputation: 209

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.

Upvotes: 0

Related Questions