Thread: NullPointerException when calling executeQuery() - why?

NullPointerException when calling executeQuery() - why?

From
rob
Date:
I'm experiencing a NullPointerException when calling executeQuery() on a
PreparedStatement  what are the possible errors I've made?

The code looks a little something like this.

...
try {

ps = con.prepareStatement(SELECT_SID);
ps.setString(1, sid);
// <-- At this point variables: ps and sid are not null and ps
// <-- seems to be properly structured as per ps.toString();
rs = ps.executeQuery(); // <- throws NullPointerException

if (rs.next())
user = resultsToObject(rs);

} catch (Throwable t) {
    t.printStackTrace();
    throw new DatastoreException(t);
}
...

The odd thing is and what may be important is that the exception is not
thrown the first time the method is called.  It is only thrown every
time after.  Is this indicitave of me improperly cleaning up from the
first call?  If so what might I have done?

After each call to the method containing the above code I do the
following:

rs.close();
ps.close();
con.close();

Then I recycle my connection reference to a pool.

Anyway a stack trace follows, if anyone could help I would appreciate
it thanks.

Rob

java.lang.NullPointerException:
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:61)
at org.postgresql.Connection.ExecSQL(Connection.java:398)
at org.postgresql.jdbc2.Statement.execute(Statement.java:130)
at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:54)
at
org.postgresql.jdbc2.PreparedStatement.executeQuery(PreparedStatement.java:99)
at mapper.UserDAO.findUser(UserDAO.java:76)
at mapper.UserBO.findUser(UserBO.java:55)
at domain.User.<init>(User.java:17)
at web.LoginAction.perform(LoginAction.java:34)
at
org.apache.struts.action.ActionServlet.processActionPerform(ActionServlet.java:1787)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1586)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:510)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

.. and the rest of the exception is probably not relevant ...


Re: NullPointerException when calling executeQuery() - why?

From
Dave Cramer
Date:
Rob,

Don't close the connection if you are sending it back to the connection
pool

Dave
On Mon, 2002-04-22 at 09:51, rob wrote:
> I'm experiencing a NullPointerException when calling executeQuery() on a
> PreparedStatement  what are the possible errors I've made?
>
> The code looks a little something like this.
>
> ...
> try {
>
> ps = con.prepareStatement(SELECT_SID);
> ps.setString(1, sid);
> // <-- At this point variables: ps and sid are not null and ps
> // <-- seems to be properly structured as per ps.toString();
> rs = ps.executeQuery(); // <- throws NullPointerException
>
> if (rs.next())
> user = resultsToObject(rs);
>
> } catch (Throwable t) {
>     t.printStackTrace();
>     throw new DatastoreException(t);
> }
> ...
>
> The odd thing is and what may be important is that the exception is not
> thrown the first time the method is called.  It is only thrown every
> time after.  Is this indicitave of me improperly cleaning up from the
> first call?  If so what might I have done?
>
> After each call to the method containing the above code I do the
> following:
>
> rs.close();
> ps.close();
> con.close();
>
> Then I recycle my connection reference to a pool.
>
> Anyway a stack trace follows, if anyone could help I would appreciate
> it thanks.
>
> Rob
>
> java.lang.NullPointerException:
> at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:61)
> at org.postgresql.Connection.ExecSQL(Connection.java:398)
> at org.postgresql.jdbc2.Statement.execute(Statement.java:130)
> at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:54)
> at
> org.postgresql.jdbc2.PreparedStatement.executeQuery(PreparedStatement.java:99)
> at mapper.UserDAO.findUser(UserDAO.java:76)
> at mapper.UserBO.findUser(UserBO.java:55)
> at domain.User.<init>(User.java:17)
> at web.LoginAction.perform(LoginAction.java:34)
> at
> org.apache.struts.action.ActionServlet.processActionPerform(ActionServlet.java:1787)
> at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1586)
> at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:510)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
>
> .. and the rest of the exception is probably not relevant ...
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
>