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.

4
  • Error at line 2734.. Did you consider refactoring code and breaking it into smaller parts? Commented Nov 24, 2013 at 18:54
  • 1
    @svz: that's the MySQL JDBC driver code, not the OP's code. Commented Nov 24, 2013 at 18:55
  • @LuluLala: the other statement assigns an alias to the sum. Why don't you do the same thing in the first statement and assign an alias to the count? Commented Nov 24, 2013 at 18:56
  • @JBNizet, huh, I missed it. Commented Nov 24, 2013 at 18:57

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

It showed this error: java.sql.SQLException: Operation not allowed after ResultSet closed
I'm sorry, I'm new to this. Do you mean the code for the connection I made for the form with MySQL?
there is one ResultSet is allowed for one Statement. may be you got Exception in this case. because you used ResultSet rs1 and rs2.
But it doesn't work if I use only one ResultSet. Is there any alternative for that?
if you use only one result set what exception you got..?
|
0

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

 while(rs1.next()){
   noproduct=rs1.getInt("count(ProductName)");
 }

2 Comments

It shows a warning that noproducts might not be initialized for the next line.
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)
0

Try this

ResultSet rs1=stmt.executeQuery("Select count(ProductName) as NoOfProducts from buy;");
rs1.next();
noproduct=rs1.getInt("NoOfProducts");

Comments

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") -  
        } 

Comments

Your Answer

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.