0

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.

LuluLala
  • 25
  • 2
  • 9

4 Answers4

1

change this to

 rs1.next();
 noproduct=rs1.getInt(1);

and see here

there is one ResultSet is allowed for one Statement. may be you got Exception in this case. because you used ResultSet res1 and rs2.

Community
  • 1
  • 1
subash
  • 3,116
  • 3
  • 18
  • 22
0

Try this

ResultSet rs1=stmt.executeQuery("Select count(ProductName) as NoOfProducts from buy;");
rs1.next();
noproduct=rs1.getInt("NoOfProducts");
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

You need rs1.next() cursor to invoke value.

 while(rs1.next()){
   noproduct=rs1.getInt("count(ProductName)");
 }
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • It shows a warning that `noproducts` might not be initialized for the next line. – LuluLala Nov 24 '13 at 19:10
  • The warning is because you declared `noproduct` without default value. Assign a default value in case the code does not execute the while block (`noproduct = 0;` before the while) – clubifaximatic Nov 24 '13 at 19:39
0

This kind of error occurs when you directly access resultset without itrating the it.

You can solve problem by itrate the resultset and get value:

while (resultSet.next()) 
        {
            JSONObject response = new JSONObject();
            response.put("dish_title", resultSet.getString("dish_title"));
            int remainingQuantity =  resultSet.getInt("quantity") -  
        }