我有以下代码块工作,但现在我收到了NPE错误。我知道这意味着代码指向一个空值,我只是不明白这是怎么发生的。它仍然有效有时,我觉得很奇怪。
我在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"));