2

JDBC Connection NullPointerException when connecting

 2 years ago
source link: https://www.codesd.com/item/jdbc-connection-nullpointerexception-when-connecting.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JDBC Connection NullPointerException when connecting

advertisements

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)


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();




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK