[英]Add items to jList
I am trying to create a method that will update a list that has already been created. 我正在尝试创建一种方法来更新已经创建的列表。 Im not sure why this is not working?
我不确定为什么这行不通?
It throws a null pointer exception. 它抛出一个空指针异常。
This is my code: 这是我的代码:
private void UpdateJList(){
String query = "SELECT * FROM names WHERE TYA=?";
String partialSearch = "Sales";
try{
connect.pst = connect.con.prepareStatement(query);
connect.pst.setString(1, partialSearch);
connect.pst.execute();
ArrayList<String> add = new ArrayList<String>();
String[] items = {};
while (connect.rs.next()){
String result = connect.rs.getString("ACNO");
add.add(result);
int length = add.size();
DefaultListModel<String> model;
model = new DefaultListModel<String>();
for (int i=0; i<length; i++){
model.add(i, result);
}
jList1.setModel(model);
jList1.setSelectedIndex(0);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Thank you in advance!! 先感谢您!!
There are 2 major problems with that code: 该代码有两个主要问题:
connect.rs.next()
because you didn't assign connect.rs with the query's resultset. connect.rs.next()
因为您没有为connect.rs分配查询的结果集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.