java.lang.NullPointerException and ResultSet

16,621

Solution 1

Rewrite the handler, like this:

waterButton.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {                                            
// TODO add your handling code here:
    try{
       Statement st = connectionObject.createStatement();

       // might want to use placeholders here as well
       String query= "SELECT itemName, itemPrice FROM item WHERE itemID = '11111'"; 
       String itemName = " ",itemPrice =" ";  

       ResultSet rs = st.executeQuery(query);

       if(rs != null){
         do { 
           itemName = rs.getString(1);
           itemPrice = rs.getString(2);
        } while (rs.next());
        orderTextArea.setText(itemName);
        orderTextArea.setText(itemPrice);
        }
      } catch (SQLException ex) { 
         System.err.println(new java.util.Date()+" : "+ex.getMessage(); 
      }
});

Solution 2

st is null. You need something like

con.prepareStatement(query);

Where con is your connection to the SQL database. You don't show how you connect to the database here. Here's a good tutorial on how to do that.

Solution 3

It looks like st is null. Your example doesn't show where st is declared, much less initialized.

You probably need to create a Statement from your query (which is just a string)

Solution 4

It look like that st is null .please check your st otherwise write the code

conn.prepareStatement(query)
Share:
16,621
JudgementFlame
Author by

JudgementFlame

Updated on June 04, 2022

Comments

  • JudgementFlame
    JudgementFlame almost 2 years

    This is my first post so please be patient. I am working on a java project and that connects to a sql database. What I am currently trying to do is when I click a button, will display the name of the item and the price of the item with the specified ID in the text area from my database. However, when I click the button an error such as this appears:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at OrderFrame.waterButtonActionPerformed(OrderFrame.java:494)
    at OrderFrame.access$000(OrderFrame.java:11)
    

    Here is the code for my waterButtonActionPerformed:

    private void waterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
        try{
    
        String query;
        query = "SELECT itemName, itemPrice FROM item WHERE itemID = '11111'";
        String itemName = " ",itemPrice =" ";  
    
          ***ResultSet rs = st.executeQuery(query);***
    
           if(rs != null){
            while(rs.next())
            { 
             itemName = rs.getString(1);
             itemPrice = rs.getString(2);
            }
           orderTextArea.setText(itemName);
           orderTextArea.setText(itemPrice);
           }
             } catch (SQLException ex) {}
    }        
    

    The line 494 is the one with the declaration of the ResultSet. I am am hoping someone can help me fix this issue. Thank you in advance.