Java + SQL - NullPointerException error

I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

code is

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

public class Bookstore extends javax.swing.JFrame {

    Connection con;
    Statement stmt;
    ResultSet rs;

    public Bookstore () {
        initComponents();
        DoConnect();
    }

    public void DoConnect() {
        try {
            String host = "jdbc:derby://localhost:1527/Database";
            String userName = "user2408";
            String password = "password2408";
            Connection con = DriverManager.getConnection(host, userName, password);

            Statement stmt = con.createStatement( );
            String SQL = "SELECT * FROM SHELF";
            ResultSet rs = stmt.executeQuery( SQL );

            rs.next();

            int ID= rs.getInt("KeyID");
            String BookID = Integer.toString(ID);
            String BookName = rs.getString("Name");
            String AuthorName = rs.getString("Author");
            String ReleaseDate = rs.getString("Release");
            String AuthorContact = rs.getString("Email");
            float Price = rs.getFloat("Price");
            String BookPrice = Float.toString(Price);

            BookIDTF.setText(BookID );
            BookNameTF.setText(BookName);
            AuthorNameTF.setText(AuthorName);
            ReleaseDateTF.setText(ReleaseDate);
            AuthorContactTF.setText(AuthorContact);
            BookPriceTF.setText(BookPrice);

        }
        catch (SQLException err) {
            JOptionPane.showMessageDialog(Bookstore.this, err.getMessage());
        }
    }
    private void NextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try {
            if (rs.next()) {  //this is where error occurs
                int ID= rs.getInt("KeyID");
                String BookID = Integer.toString(ID);
                String BookName = rs.getString("Name");
                String AuthorName = rs.getString("Author");
                String ReleaseDate = rs.getString("Release");
                String AuthorContact = rs.getString("Email");
                float Price = rs.getFloat("Price");
                String BookPrice = Float.toString(Price);

                BookIDTF.setText(BookID );
                BookNameTF.setText(BookName);
                AuthorNameTF.setText(AuthorName);
                ReleaseDateTF.setText(ReleaseDate);
                AuthorContactTF.setText(AuthorContact);
                BookPriceTF.setText(BookPrice);
            }
            else {
                rs.previous();
                JOptionPane.showMessageDialog(Bookstore.this, "End of File");
            }
        }
        catch (SQLException err) {
            JOptionPane.showMessageDialog(Bookstore.this, err.getMessage());
        }
    }

Java throws NullPointerException (usually abbreviated NPE) when you try to invoke a method or reference a property on an object that is null.

I've read this 10 times already and I still have no clue what could be wrong.

If I would comment/remove the next button function it would work. Constructor loads first row from the database into the app. What I want to understand is why it doesn't work for the next function. Mentions: Table has 3 entries so there is a next entry to go to which means the table is not the reason for NPE

2 answers

  • answered 2018-01-11 19:51 Elysiumplain

    Looks like you are creating another instance of ResultSet that is scoped differently at ResultSet rs = stmt.executeQuery( SQL );

  • answered 2018-01-11 19:51 FredK

    ResultSet rs = stmt.executeQuery( SQL );
    

    The above line in DoConnect makes rs a local variable to that function. This is not the same variable as the rs declared as an instance variable, and it goes out of scope at the end of that function . Thus in NextButtonActionPerformed, rs (the instance varia ble) is still null.