I'm a newbie so please forgive if this is a simple programming error,
but ...
I am getting an error while trying the simplest of queries to my DB. I
can't get the result back from the ResultSet, I keep getting
NullPointerExceptions every time I try any of the ResultSet.getXXX()
methods. But if check the MetaData of the ResultSet I see that it is
does contain one row ...
Can someone tell me what I am doing wrong or point me in the right
direction?
Here's part of my code and the output:
Code:
----------
public String getNumberOfMembers() {
int cols = 0;
String count = "0";
Object o;
sql = "select count(id) from members";
connect();
try {
res = stmt.executeQuery(sql);
System.out.println("Ok query " + sql);
}
catch (Exception e) {
System.out.println("Error executing query " + sql);
}
if (res == null) System.out.println("NULL");
else System.out.println("NOT NULL");
try {
ResultSetMetaData dat = res.getMetaData();
cols = dat.getColumnCount();
}
catch (SQLException e) {
System.out.println("Error getting MetaData");
}
System.out.println("Col count is " + cols);
try {
o = res.getObject("count");
}
catch (SQLException e) {
}
close();
return count;
}
Output:
----------
Ok query: select count(id) from members
NOT NULL
Col count is 1
java.lang.NullPointerException
at org.postgresql.jdbc2.ResultSet.getObject(ResultSet.java:730)
at org.postgresql.jdbc2.ResultSet.getObject(ResultSet.java:803)
at TAL.MemberDB.getNumberOfMembers(MemberDB.java:40)
at
TAL.MemberSearchPanel.jcInitComponents(MemberSearchPanel.java:64)
at TAL.MemberSearchPanel.<init>(MemberSearchPanel.java:37)
at TAL.Tabs.<init>(Tabs.java:11)
at TAL.Main.main(Main.java:30)