0

I'm almost sure that there is a very simple explanation why my codes are not behaving as I thought they would but basically my problem is that the resultSet still go through the code block that it should not go so it comes back with an error because the rs is empty.

To explain further, my sql statement retrieves data with records of the selected date and payment method.

If no data, it should just display 0.00 but it does not.

It still goes through after if(rs.next()) - an explanation on why my codes behave like this would be greatly appreciated.

Thank you.

pstmt = conn.prepareStatement("Select sum(bdPrice) from tblReceipt where date=? and paymentMethod=?");
pstmt.setString(1, String.valueOf(tdf.format(datePicker.getDate())));
pstmt.setString(2,"CARD");
rs = pstmt.executeQuery();
if (rs.next()) {
 lblDailyCard.setText(String.valueOf(df.format(Double.valueOf(rs.getString(1)))));
} else {
 lblDailyCard.setText("0.00");
}
pstmt.close();
broc.seib
23.1k9 gold badges70 silver badges65 bronze badges
asked Sep 14, 2017 at 23:31
3
  • 1
    Can you include the exception? Commented Sep 14, 2017 at 23:36
  • @MitchWheat I just did and it comes back null which I know but why does it not jump to "else" in my codes? is there something so obvious that I'm not seeing? Thanks. Commented Sep 14, 2017 at 23:39
  • @ChrisWitteveen Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException - that;s what I get. Commented Sep 14, 2017 at 23:41

1 Answer 1

2

I think this might be the problem:

You try to select sum(bdPrice) which is a integer/double, yet when you try to set the text, you use rs.getString(1). This should be rs.getDouble(1) since the sum is a double (assuming that bdPrice is also a double).

answered Sep 14, 2017 at 23:50
Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow! I feel like an idiot for missing that. It's almost 3am here and could not get to sleep because it bugs me. This solved it. Thanks a lot!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.