1

I'm working on a program where I need to run the following.

SELECT questionID FROM TableX WHERE validAudit = 1;

I'm able to connect to the database in question, and have even managed to successfully insert data to it. The problem is that the SELECT statement from above should return 118 results to my results set. Instead, I get one result at best, and the program crashes with a null pointer exception.

public static List<Integer> getQuestionID() throws SQLException{
        Connection conn = null;
        Statement stmt = null;
        List<Integer> questionIDs = null;
        String query = "SELECT questionID FROM Questions WHERE GAIG_AMIG = 1;";

        try{
            conn = getDBConnection();
            stmt = conn.createStatement();
            ResultSet IDs = stmt.executeQuery(query);

            while(IDs.next())
            {
                int id = IDs.getInt("questionID");
                JOptionPane.showMessageDialog(new JFrame(), "Current questionID is " + id, "Question ID", JOptionPane.INFORMATION_MESSAGE);
                questionIDs.add(id);
            }
            IDs.close();
            return questionIDs;
        }catch(SQLException e){
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage());
        }finally{
            if(stmt != null)
                stmt.close();
            if(conn != null)
                conn.close();
        }
        return questionIDs;
    }

Here's the method where it fails. The program specifically fails at:

questionIDs.add(id);

I've looked through Oracle's documentation on this, as well as here on SO. I haven't found an instance exactly like mine, and I can't get it to properly pull the information that I know is there.

SHOW ME WHERE IT'S DUPLICATED. I've looked through all the SO questions about JDBC MySQL that I could find, and NONE of them matched this exact scenario.

  • Hmmm. I wouldn't expect it to hang, but I'm pretty sure you should be getting a NullPointerException from ```questionIDs.add(id);``` because you never actually initialized a List to take the results. I think you need to correctly initialize that collection like ```List questionIDs = new ArrayList();``` which will allow your ```questionIDs.add(id);``` to work. – Bob Kuhar Oct 03 '16 at 20:00

0 Answers0