0

I'm trying to set up a little JDBC project and I'm running into an error which is confusing me.

It's a Vocabulary and Multiple Choice Trainer. I've got the Multiple Choice running fine but a snipped I nearly copied from it just won't work for my vocabulary-part.

Here it is:

private void createIDList() throws ClassNotFoundException, SQLException {

    Class.forName("org.apache.derby.jdbc.ClientDriver");
    con = DriverManager.getConnection("jdbc:derby://localhost:1527/DataBase");
    stmt = con.createStatement();
    String s_s = "SUB LIKE '%";
    s_s = s_s + db_s.get(0) + "%' ";
    db_s.remove(0);
    while (!db_s.isEmpty()) {
        s_s = s_s + "OR SUB LIKE '%" + db_s.get(0) + "%' ";
        db_s.remove(0);
    }
    String s_t = "TOPIC LIKE '%";
    s_t = s_t + db_t.get(0) + "%' ";
    db_t.remove(0);
    while (!db_t.isEmpty()) {
        s_t = s_t + "OR TOPIC LIKE '%" + db_t.get(0) + "%' ";
        db_t.remove(0);            
    }
    rs = stmt.executeQuery("SELECT ID FROM APP.Voc WHERE (" + s_t + ") AND (" + s_s + ")");

    while (rs.next()) {
        id.add(rs.getInt("ID")); //Exception is here java.lang.NullPointerException
    }}

Here is the part which happens before:

public class Session extends javax.swing.JFrame implements KeyListener {
int p_r = 0;
int p_w = 0;
int currentID = -1;
private List db_t, db_s;
private List<Integer> id;
Connection con;
Statement stmt;
ResultSet rs;
String from = "V_ORIG";
String to = "V_DEST";
String ans;

/**
 * Creates new form Session
 */
public Session(List p_db_t, List p_db_s) {
    db_t = p_db_t;
    db_s = p_db_s;
    initialize(); //-->

And initialize():

private void initialize() {
    try {
        createIDList();
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Session.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(Session.class.getName()).log(Level.SEVERE, null, ex);
    }
    Collections.shuffle(id);
}

I do receieve following error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Session.createIDList(Session.java:520)
at Session.initialize(Session.java:489)
at Session.<init>(Session.java:45)
at DB_Selector.bt_goActionPerformed(DB_Selector.java:193)
at DB_Selector.access$100(DB_Selector.java:18)
at DB_Selector$2.actionPerformed(DB_Selector.java:83)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

So much so good, but what is confusing me is following: First: Why does it enter the while(rs.next()) loop. Shouldn't rs.next() return false before running into an NullPointer? And: I can get the number of columns with rs.getMetaData().getColumnCount(). So there has to be some kind of communication between the database and the ResultSet.

When I execute the Querys manually, I do get a list of ID's.

Where could be the error here?

If you do need more information, feel free to ask.

luk2302
  • 46,204
  • 19
  • 86
  • 119

1 Answers1

2

Your id is null.

Use

id = new ArrayList<>();
while (rs.next()) {
    id.add(rs.getInt("ID"));
}
luk2302
  • 46,204
  • 19
  • 86
  • 119
  • My god, this is so simple how could I oversee this. Thank you a LOT! I've wasted so much time on this. –  Dec 01 '17 at 19:16