Answered step by step
Verified Expert Solution
Question
1 Approved Answer
solve with application class. Error: Exception in thread main java.lang.NullPointerException : Cannot read the array length because scores is null The file provides information of
solve with application class.
Error: Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "scores" is null
The file provides information of students in a course, their id #, names, and grades. However, I have to create a new student who isnt apart of the Course.txt file. And when creating an student object i received errors. and when using the deleteStudent method it does not display "Delete student 666-66-6666. Number of students in the course: " (theres nothing wrong with the deleteStudent method)
package lab10; import java.io.*; import java.util.*; public class Lab11App { public static void main(String[] args) throws Exception { // Instantiate a Course object, referenced by the Course, using the default // constructor Course theCourse = new Course(); // Instantiate a Scanner object, referenced by fileScan, using the parameterized // constructor File courseData = new File("courseData.txt"); Scanner fileScan = new Scanner(courseData); // Read the first line in the file which contains the number of students in the // file and store the value appropriately int numOfStudents = Integer.parseInt(fileScan.nextLine()); // Write a loop that will iterate through the students in the file (ie. the // number you read in #6 for (int i = 0; i < numOfStudents; i++) { // Write code to read the id, last name and first name from the file and store // them appropriately String id = fileScan.nextLine(); String firstName = fileScan.nextLine(); String lastName = fileScan.nextLine(); // Write code to read the four exam grades and store them in an array of doubles double[] grades = new double[4]; for (int j = 0; j < 4; j++) { grades[j] = Double.parseDouble(fileScan.nextLine()); } // Create a new Student object using the parameterized constructor passing the // values read from the file Student student = new Student(id, lastName, firstName, grades); // Insert this reference to a Student object into the Course. theCourse.addStudentAtAnyPosition(student, i); } // After all student data has been read from the file, call the close method on // the Scanner object fileScan.close(); // You should now have students in the course. To display them in a dialog box, // type the following statement System.out.println("Students in the course: " + theCourse.toString()); // Call the findStudent method for the following students in the Course Student student1 = new Student("123-45-6789", "Homer", "Simpson", new double[] { 80.0, 55.0, 65.0, 40.0 }); Student student2 = new Student("765-43-2100", "Todd", "Flanders", new double[] { 77.0, 79.0, 82.0, 66.0 }); Student student3 = new Student("999-99-9999", "Montgomery", "Burns", new double[] { 100.0, 100.0, 100.0, 50.0 }); Student student4 = new Student("666-66-6666",null,null,null); // Display the value returned for each of the test cases System.out.println("Position of the student with id 123-45-6789: " + theCourse.findStudent(student1)); System.out.println("Position of the student with id 765-43-2100: " + theCourse.findStudent(student2)); System.out.println("Position of the student with id 999-99-9999: " + theCourse.findStudent(student3)); System.out.println("Position of the student with id 666-66-6666: " + theCourse.findStudent(student4)); if (theCourse.deleteStudent(student2)) { System.out.println("Delete student 765-43-2100. Number of students in the course: " + theCourse.getNumStudents()); } if (theCourse.deleteStudent(student3)) { System.out.println("Delete student 999-99-9999. Number of students in the course: " + theCourse.getNumStudents()); } if (theCourse.deleteStudent(student1)) { System.out.println("Delete student 123-45-6789. Number of students in the course: " + theCourse.getNumStudents()); } if (theCourse.deleteStudent(student4)) { System.out.println("Delete student 666-66-6666. Number of students in the course: " + theCourse.getNumStudents()); } System.out.println("Students in the course: " + theCourse); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
1 NullPointerException for student4 The error javalangNullPointerException Cannot read the array len...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started