【发布时间】:2011-10-16 08:41:59
【问题描述】:
我有一个函数“getStudent()”,它返回一个字符串的 ArrayList,当我在另一个类中调用这个函数时,我得到一个 NullPointerException,我以为我已经正确初始化了我的 List。
这是两个函数,我得到 NullPointerException 的行是粗体的。
public ArrayList<String> getStudents(){
try
{
System.out.println("gets here ");
Statement newStat = this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet res = newStat.executeQuery("SELECT FirstName FROM Students");
String data = "";
while (res.next()){
studentList.add(res.getString("FirstName"));
}
}
catch(SQLException e){
System.err.println("SQLException: " + e.getMessage());
}
return studentList;
}
调用“getStudents()”的函数
public class CancelListener implements ActionListener{
private Main_Menu menu;
private ArrayList<String> arrayList = new ArrayList<String>();;
Iterator iterator = arrayList.iterator();
public CancelListener(Main_Menu menu) {
this.menu = menu;
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equalsIgnoreCase("Cancel")){
**arrayList = StudentModel.getStudents();**// NULLPOINTER EXCEPTION
while(iterator.hasNext()){
System.out.println(iterator.next().toString());
}
this.menu.resetStudent();
}
}
}
【问题讨论】:
-
能否请您发布完整的堆栈跟踪?
标签: java iterator arraylist nullpointerexception resultset