当我尝试运行这些方法时,我只得到一个关于nullpointexception的错误,而我也一直在检查我的代码并将它与类似的应用程序进行比较,但我仍然不明白。
编辑: 我在GUI代码中的for循环行上遇到异常。
我在MVC中构建了我的应用程序
GUI代码:
public void actionPerformed(ActionEvent e) {
String pnr = textResPnr.getText();
String courseNr = textResCourseNr.getText();
ArrayList<Student> studentList = controller.results(pnr, courseNr);
String resultString = "";
for (Student student : studentList) {
resultString = student.getPnr() + " " + student.getName() + " " + student.getGrade() + "\n";
}
textAreaResultat.setText(resultString);
}
我的控制器:
public ArrayList<Student> results(String pnr, String courseNr){
try {
student.setPnr(pnr);
course.setCourseNr(courseNr);
ArrayList<Student> studentList = null;
if(student.getPnr() != null){
dal.getResults(course, student);
}else{
studentList = dal.getAssignedStudents(course);
/*for (Student student : studentList) {
}*/
}
return studentList;
} catch (Exception e) {
return null;
}
}
我的数据库访问层:
public ArrayList<Student> getAssignedStudents(Course course){
try {
Statement stmt = connection.createStatement();
ResultSet rset = stmt.executeQuery("SELECT Student.pnr, Student.namn, Registrering.betyg FROM Registrering INNER JOIN Student ON Registrering.pnr = Student.pnr WHERE betyg IS NOT NULL AND kursnr = '" + course.getCourseNr() + "';");
ArrayList<Student> studentList = new ArrayList<Student>();
while (rset.next()) {
String pnr = rset.getString("pnr");
String name = rset.getString("namn");
String grade = rset.getString("betyg");
Student student = new Student();
student.setName(name);
student.setPnr(pnr);
student.setGrade(grade);
studentList.add(student);
}
return studentList;
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
return null;
}
}