-1

I have a SQL database which we managed to get connected using the proper drivers. Firstly here's the code

package com.Derp.test.blah;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.io.PrintWriter;


public class DBConnect {
    String databaseURL;
    String user;
    String password;

public DBConnect(String inurl, String inuser, String inpass) {
    databaseURL = inurl;
    user = inuser;
    password = inpass;
}
@SuppressWarnings("null")
public List<String> dbConnector() throws ClassNotFoundException {
    System.out.println("dburl: "+databaseURL);
    System.out.println("user: "+user);
    System.out.println("pass: "+password);
    List<String> imgPaths = null;
    Connection conn = null;
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection(databaseURL, "service_manager", "watermelon");
        if (conn != null) {
            System.out.println("Connected to the database");
            Statement s = conn.createStatement();
            //Count the amount of users in the Accounts table with the user name of the query
            System.out.println("Before executeQuery");
            ResultSet rs = s.executeQuery("SELECT * FROM testable");
            while(rs.next()){
                System.out.println(rs.getString("imgpath"));
                imgPaths.add(rs.getString("imgpath"));
            }
            System.out.println("Size of Images: " + imgPaths.size());
            //return imgPaths;
        }
    } catch (SQLException ex) {
        System.out.println("An error occurred. Maybe user/password is invalid");
        ex.printStackTrace();
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
    return imgPaths;

Ignore how horrible some parts of this code is please. In the while loop

System.out.println(rs.getString("imgpath"));

Is printing out results to the console that are in the SQL database, however I am unable to add the results to the imgPaths String List, and am getting the following error.

Jan 30, 2019 10:20:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.Derp.test.blah.TestServlet] in context with path [/BlahTest] threw exception
java.lang.NullPointerException
    at com.Derp.test.TestBlah.DBConnect.dbConnector(DBConnect.java:39)
    at com.Derp.test.TestBlah.TestServlet.doGet(TestServlet.java:140)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

Null pointer exception... Any help would be greatly appreciated.

Ostap Hnatyuk
  • 1,116
  • 2
  • 14
  • 20

1 Answers1

1

Your imgPaths variable is initialized to null. Change the initialization to a list object then it would allow you to add items.

Gopi
  • 620
  • 8
  • 16