0

first things first this is not a duplicate as i have seen everey similar question i found

so what im doing is im conning to a data base in a maven project

 i have 2 concerns -

the build gives the following error

SEVERE: Servlet.service() for servlet [webapp.login.loginServlet] in context with path [] threw exception
java.lang.NullPointerException: Cannot invoke "java.sql.Connection.createStatement()" because "con" is null
    at webapp.login.loginServlet.doGet(loginServlet.java:46)
    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.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    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:100)
    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:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at java.base/java.lang.Thread.run(Thread.java:832)

i made my connection null and later intilized it ...in a try catch

request.getRequestDispatcher("/WEB-INF/veiws/login.jsp").forward(request, response);
            // connect
            
                    Connection con = null;
                    try {
                        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/to_be_done","root","");
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
if i remove null compile error here

try {
                        st = con.createStatement();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
}   

"con" is not initialized

and if i leave it null and ignore the error i get

HTTP Status 500 - Cannot invoke "java.sql.Connection.createStatement()" because "con" is null

  • 1
    If `con` is still `null` that means either your code line that initializes it is never reached in the program flow, or that it is reached but throws an exception. Have you checked your log to see if creating the connection triggers an exception that you print to it with `e.printStackTrace();`? – OH GOD SPIDERS Apr 13 '21 at 12:39
  • nan the debug of eclipse says it runs normal... except of the maven build – rohan_code Apr 13 '21 at 12:40
  • Don't just catch exception and then continue on as if nothing happens. Clearly `DriverManager.getConnection` threw an exception, so `con` is still null. – Mark Rotteveel Apr 13 '21 at 16:25

0 Answers0