0

When i try to run these methods then i only get an error about nullpointexception while i've also have been checking my code and comparing it to similar applications but still i don't understand.

EDIT: I get an exception on the for loop line in the GUI code.

Ive built my application in MVC

GUI code:

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);

        }

My controller:

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;
    }
}

My Database Access Layer:

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;
    }
}
Danny D
  • 33
  • 2
  • 8
  • 1
    Ignoring exceptions is a no-no. – Sotirios Delimanolis Sep 30 '15 at 00:12
  • It means that you're doing the coding equivalent of driving your car with your eyes closed. At least print out the stacktrace in your catch block. Find out **why** the exception is being thrown. – Hovercraft Full Of Eels Sep 30 '15 at 00:13
  • You will want to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Sep 30 '15 at 00:16

0 Answers0