0

I've made a simple java class which connects to the database and prints out some data. When I debug, my class stops where I try to establish a connection and throws the exception NullPointerException.

This is my code

public class test_con {
    public static void main(String[] args) throws SQLException {
        OdbcConnection dbcon = new OdbcConnection();
        Connection con = dbcon.getConnection();
        String query = "SELECT Client Code FROM Clients";
        Statement stmt =con.createStatement();
        ResultSet resl = stmt.executeQuery(query);

        while(resl.next()){
            System.out.println(resl.getString("Client Code"));
        }
    }
}

and my connection code is

public Connection getConnection() {

    Connection con = null;

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String connectionUrl = "jdbc:odbc:SynergistConnection";

        con = DriverManager.getConnection(connectionUrl, "******", "******");
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("SQL Exception: " + e.getMessage());
    } catch (ClassNotFoundException cE) {
        System.out.println("Class Not Found Exception: " + cE.toString()); 
    }
    return con;
}

Has anybody encountered any similar problem or knows how to solve this?

stacktrace:

java.lang.NullPointerException
at org.apache.jsp.test_005flaur_jsp._jspService(test_005flaur_jsp.java:124)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Laurence Nicolaou
  • 569
  • 2
  • 8
  • 28

2 Answers2

0

It seems that your getConnection() method is throwing an exception. That's why Connection object con is not getting intialized. Consequently, you are getting NullPointerException at line:

 Statement stmt =con.createStatement();
Touchstone
  • 4,794
  • 7
  • 36
  • 47
0

The sun.jdbc.odbc.JdbcOdbcDriver class has been dropped in Java 8. (See this Answer.)

Your code, as written, will catch a ClassNotFoundException (if caused by the above), and return a null. (Bad, bad, bad ...)

But the caller does this:

    Connection con = dbcon.getConnectionSynergist2();
    ...
    Statement stmt = con.createStatement();

Assuming that getConnectionSynergist2 is the getConnection method (and you've made a mistake copying the code into the Question), then you are liable to call createStatement on a null.

Community
  • 1
  • 1
Stephen C
  • 632,615
  • 86
  • 730
  • 1,096