Hi Forum,

I am working with an embedded derby DB.
I have a query that will total a column from a table (bigDecimal values) and
cast the result to a string.  Then return that value.  All works well if
there is at least one row in the table that meets the query requirements. 
However, if there are no rows...I get a null pointer error.

I am using
if (resultset.next()) {
do something with the result set
}

Is there a way to to check this that I need to do?

Here is the code in question...

protected String getSalesTotals(String tableName, String method) {
                String strTotal = null;
                ResultSet rs = null;
                PreparedStatement psTotal = null;
                
                try {
                        if (method.equals("All")) {
                                psTotal =
                                        conn.prepareStatement("select 
sum(AMT_PAID) from " + tableName + "
where TRANS_DATE=?");
                                psTotal.setDate(1, new java.sql.Date(new 
Date().getTime()));
                                rs = psTotal.executeQuery();
                        }
                        else {
                                psTotal = conn.prepareStatement("select 
sum(AMT_PAID) from " + tableName
+ " where TRANS_DATE=? and PAYMENT_METH=?");
                                psTotal.setDate(1, new java.sql.Date(new 
Date().getTime()));
                                psTotal.setString(2, method);
                                rs = psTotal.executeQuery();
                        }
                }
                catch (SQLException e) {
                        e.printStackTrace();
                }
                
                
                try {
                        if (rs.next()) {
                                strTotal = rs.getBigDecimal(1).toString();
                        }
                }
                catch (SQLException e) {
                        e.printStackTrace();
                }
                
                return strTotal;
        }

Any and all pointers, advice, and assistance is greatly appreciated.
Mike

-- 
View this message in context: 
http://www.nabble.com/Checking-A-ResultSet-tp22005209p22005209.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.

Reply via email to