I've got this form which contains three text fields and two radio buttons. The form is supposed to calculate the bill amounts for a customer. I've written the following code and it shows a NullPointerException error.
int noproduct;
double totalprice, price, shipcharge;
try{
Connection connection=getConnection();
stmt=connection.createStatement();
ResultSet rs1=stmt.executeQuery("Select count(ProductName) from buy;");
ResultSet rs2=stmt.executeQuery("Select sum(price) as TPrice from buy;");
noproduct=rs1.getInt("count(ProductName)");
NoProducts.setText(""+noproduct);
price=rs2.getDouble("TPrice");
shipcharge=3 * noproduct;
ShipCharges.setText(""+shipcharge);
totalprice=price+shipcharge;
TotalPrice.setText(""+totalprice);
if(CashRB.isSelected()){
totalprice=totalprice+(5*noproduct);
JOptionPane.showMessageDialog(null,"You have paid the bill by Cash!");
}
else if(CCardRB.isSelected()){
totalprice=totalprice-(5*noproduct);
JOptionPane.showMessageDialog(null,"You have paid the bill using Credit Card!");
}
}
catch(Exception ex){
System.out.println(ex.toString());
ex.printStackTrace();
}
finally{}
This line in specific shows the error:
noproduct=rs1.getInt("count(ProductName)");
I need an alternate code to display the same result as this.
As well as it shows these errors, which I don't really understand what they are:
java.lang.NullPointerException
at com.mysql.jdbc.ResultSetImpl.buildIndexMapping(ResultSetImpl.java:709)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1069)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2734)
Can anyone help me please? Thank you in advance.