简体   繁体   中英

Java App Does Not Load Data from PostgreSQL

My Java app works fine locally, but now when I have deployed it on an EC2 instance (Ubuntu 14, Tomcat 8, PostgreSQL 9.3), I am getting the following error (see below). The back-end was written by another developer who is no longer part of the team. Basically, it needs to display some data once a value is chosen from a dropdown menu.

I've been Google-ing this issue like crazy but have found no solution. I was thinking it may be related to the jdbc:postgresql://localhost:5432/dbName . I even tried inserting the actual IP instead of localhost but still no luck. To me, it seems like an issue with connecting with the database rather than with the code. As I mentioned, it is running fine locally on my computer.

Any guidance will be greatly appreciated as I am not a Java expert. Thanks for your help.

The full error log is below:

java.lang.NullPointerException
com.app.data.ConnectionPool.freeConnection(ConnectionPool.java:52)
com.app.data.appDB.getData(appDB.java:155)
com.app.servlets.GetAppServlet.processRequest(GetAppServlet.java:42)
com.app.servlets.GetAppServlet.doGet(GetAppServlet.java:67)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

Based on what little you have provided, my best guess is that the connection.close() method you mention is being called in a finally block where there isn't any check to see whether connection is null .

I imagine your code looks something like the following:

Connection connection = null;
try {
    connection = getDatabaseConnectionSomehow(...);
    // do stuff with database
}
finally {
    connectionPool.freeConnection(connection);
}

If the calll to getDatabaseConnectionSomehow(...) throws an exception, then connection will be null when the finally block is entered. The code I presented above doesn't check to see whether connection is null , and if your freeConnection method also fails to check whether the connection is null before closing it, you will get a NullPointerException.

An exception thrown from within a finally block will replace any exception thrown within the corresponding try block. So in your case, you don't see the real exception getting the database connection because the NullPointerException gets in the way.

Hopefully, adding a suitable null-check should help you figure out what the real problem is here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM