简体   繁体   中英

NullPointerException from Connection.preparedStatement for SQLite JDBC

what I'm trying to do is get a ResultSet from a SQL statement from which then I can derive certain values. What I seem to be having troubles with is the preparing of the prepared statement which occurs at this line.

String sql = "SELECT " + dataState + " FROM " + table + whereState + ";";
java.sql.PreparedStatement prep = conn.prepareStatement(sql); // Error here

I've already checked that the variable conn is not null. All the values in the statement are not null and the stack trace shows this.

java.lang.NullPointerException
at org.sqlite.PrepStmt.(PrepStmt.java:37)
at org.sqlite.Conn.prepareStatement(Conn.java:231)
at org.sqlite.Conn.prepareStatement(Conn.java:224)
at org.sqlite.Conn.prepareStatement(Conn.java:213)
at org.utilities.storagemethods.SQLiteMethod.load(SQLiteMethod.java:223)
at org.utilities.DataManager.load(DataManager.java:97)
at org.utilities.Test.main(Test.java:21)

A print of the sql statement shows that it gives this.

SELECT testString, testBool, testObj FROM testTable WHERE testInt=?;

You can ascertain the values from the given string.

What I can't figure out is what I'm doing wrong to cause this, if you're curious about what sqlite system I'm using, you can find the home page here: http://www.zentus.com/sqlitejdbc/

Thanks in advance for any help on this. I'm completely stumped.

Note: In an attempt to save resources, I was caching my Connection's, and re-using them later. When I try re-getting the connection, it works perfectly. Is there a way I can cache them or do I need to load them every time I want to use it?

I was getting this error, and it was caused because I had closed the connection. The connection wasn't null, it was just closed. ARRRGGG! Like the OP hinted at, I had started with the sample code at Zentus (Xerial now, since Zentus seems to have shut down). The sample code has a finally block where he closes the connection. Once I took out the finally block, my error disappeared. Just remember to close the connection before you exit the program.

To see if you have the same problem, right before the line that gives the error, try adding

System.out.println("isClosed = " + conn.isClosed());

If not wrong, I think it is because of ';' inside your SQL statement. Please remove and try again ?

Regards, Code Behind

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