Nullpointer Exception using ArrayList with ResultSet
I have a function "getStudent()" that returns an ArrayList of strings and when i call this function in another class, i get a NullPointerException, i thought i had correctly initialized my List.
Here are the two functions and the line i get a NullPointerException is in bold.
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;
}
Function that calls 'getStudents()'
public class CancelListener implements Action开发者_如何转开发Listener{
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();
}
}
}
Your StudentModel
variable is probably null. The code you posted doesn't show how that variable is initialized so I can't tell you exactly what you're doing wrong, but when you reach the line marked it must be null.
You should check that you are initializing that variable correctly before you try to use it.
You probably didn't initialize StudentModel
. But I can't tell for sure since this part of the code doesn't appear here.
getStudents() isn't static, and it looks like you're calling it as a static method (judging by the way you've capitalized StudentModel.) So, as others have already said, you probably need to instantiate a StudentModel object, then call instance.getStudents().
Further, I don't see where you're creating an array instance. getStudents() adds items to studentList, but we don't see how studentList is initialized. This might also be the problem. Frankly, given what you're doing, there's probably no reason to make studentList an instance variable. So just declare the variable locally and allocate the array in the getStudents() method.
You mentioned that you think you initialized the list correctly, but this isn't what you are getting the exception on. Also, you may have another problem.
You get an iterator for your list:
Iterator iterator = arrayList.iterator();
Then you assign a different object to that reference:
arrayList = StudentModel.getStudents();// NULLPOINTER EXCEPTION
Then you try to use the iterator:
while(iterator.hasNext()){
My understanding is that this shouldn't cause an exception, since you're not making changes to list the iterator refers. But, you are almost certainly iterating through a different list that you think you are. Is it possible you may be mis-interpreting an exception that is somehow caused here?
精彩评论