Scanner sc = new Scanner(System.in);ArrayList students = new ArrayList();ArrayList courses = new ArrayList();// Method to read the file contents and stores it in// instance arraysvoid readStudents(){// Scanner class object declaredScanner readStuF = null;// try block beginstry{// Opens the file for readingreadStuF = new Scanner(new File("student.txt"));// Loops till end of the file to read recordswhile(readStuF.hasNextLine()){String stu = readStuF.nextLine();String []eachStu = stu.split(" ");students.add(new Student(eachStu[0], eachStu[1],eachStu[2], Long.parseLong(eachStu[3]),Integer.parseInt(eachStu[4]), Integer.parseInt(eachStu[5]),Integer.parseInt(eachStu[6])));}// End of while loop}// End of try block// Catch block to handle file not found exceptioncatch(FileNotFoundException fe){System.out.println("\n ERROR: Unable to open the file for reading.");}// End of catch block// Close the filereadStuF.close();}// End of methodI followed this code and for some reason its showing the error in the image. i cant figure out whats wrong, please help!Extracted text: Client (1) [Java Application] C:\Program Files\Java\jdk-16.0.1\bin\javaw.exe (28 Jul 2021, 8:39:54 pm – 8:39:54 pm) ERROR: Unable to open the file for reading. Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Scanner.close()" because "readStuF" is null at Client.readStudents (Client.java:182) at Client.main(Client.java:415)
// Close the filereadStuF.close();}// End of method
I followed this code and for some reason its showing the error in the image. i cant figure out whats wrong, please help!
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here