【发布时间】:2013-02-27 14:22:31
【问题描述】:
我试图通过比较表中的数据和来自 myframe 的数据来加载到组合框,但是每次我的代码运行时我都会收到以下错误消息:- java.lang.NullPointerException,我也尝试使用向量。请帮助:这是我的数据库类中的代码
public ArrayList allocateStaffcombobox(Allocation aloc) throws SQLException
{
ArrayList<Allocation> vec = new ArrayList<Allocation>();
String sql = "select * from staffsubalocation where SubCode='"+aloc.getSubjCode()+"'";
ResultSet result = stmt.executeQuery(sql);
while (result.next())
{
String staffNo = result.getString("StaffNo");
String subcode=result.getString("SubCode");
System.out.println(staffNo+" "+ subcode);
vec.add(new Allocation(staffNo,subcode));
}
return vec;
}
这是来自我的 java 类的代码:
public void loadStaffCombo()
{
try
{
DatabaseManager db = new DatabaseManager();
ArrayList<Allocation> sub=db.allocateStaffcombobox(null);
Staffcombobox.removeAllItems();
// String firsIndex = " ";
for(int x = 0; x< sub.size(); x++)
{
Staffcombobox.addItem( sub.get(x).getStaffNo());
}
}
catch (SQLException ex)
{
Logger.getLogger(ViewSubjectsJInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
【问题讨论】:
-
你能发布错误堆栈跟踪吗?
-
您在
loadStaffCombo方法中的声明ArrayList<Allocation> sub = db.allocateStaffcombobox(null);中明确传递null,这肯定会给您一个NullPointerException -
我该如何解决这个问题(在该语句中不传递 null
标签: java sql arrays swing jdbc