我正在尝试设置一个小JDBC项目,并且遇到了一个使我感到困惑的错误。
这是词汇和多项选择培训师。我的多项选择正常运行,但是我几乎从中复制了一个剪刀,只是对我的词汇零件无法使用。
这里是:
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
}}
这是以前发生的部分:
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(); //-->
和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);
}
我确实收到以下错误:
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)
太好了,但是让我感到困惑的是:首先:为什么它输入whier(rs.next())循环。rs.Next()不应该在遇到nullpointer之前返回false吗?和:我可以使用Rs.GetMetadata()。getColumnCount()获得列数。因此,数据库和结果集之间必须进行某种交流。
当我手动执行查询时,我确实会得到ID的列表。
这里的错误在哪里?
如果您确实需要更多信息,请随时提出。
您的id
是null
。
使用
id = new ArrayList<>();
while (rs.next()) {
id.add(rs.getInt("ID"));
}