0

I had the following chunk of code working, but now I am getting a NPE error. I know this means the code is pointing at a null value, I just don't understand how this would happen out of nowhere. It also still works sometimes, which I find very strange.

I get the null pointer error on the rs = myStmt.... line

public static void listClasses() {
        System.out.println("\nStudent Enrollment\n");
        try {
            System.out.println("Enter Student ID to See What Classes they are enrolled in: ");
            user_entered_student_id = sc.nextLine();

            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "root", "");

            boolean found = false;
            String listStudentEnrollmentInfo =
                    "SELECT ClassSelector.student_x_class.student_id, " +
                            "ClassSelector.students.student_name, " +
                            "ClassSelector.students.hometown, " +
                            "ClassSelector.classes.class_name, " +
                            "ClassSelector.classes.description " +
                            "FROM ClassSelector.student_x_class " +
                            "JOIN classes on ClassSelector.student_x_class.class_id = ClassSelector.classes.class_id " +
                            "JOIN students on ClassSelector.student_x_class.student_id = ClassSelector.students.student_id " +
                            "Where ClassSelector.student_x_class.student_id =" + user_entered_student_id;
            rs = myStmt.executeQuery(listStudentEnrollmentInfo);
            while (rs.next()) {
                String studentInClass = (rs.getString("student_id") + "\t" + rs.getString("student_name") + " \t " + rs.getString("class_id") + " \t" + rs.getString("class_name"));
J. Doe
  • 33
  • 7

0 Answers0