After I ran DriverExamDemo, I got an error message in the command box:
I don't know what I am doing wrong here.HTML Code:Exception in thread "main" java.lang.NullPointerException at DriverExam.<init>(DriverExam.java:16) at DriverExamDemo.main(DriverExamDemo.java:18) Press any key to continue . . .
public class DriverExamDemo { public static void main(String[] args) { String[] answersGiven1 = {"B","D","B","A","C", "A","B","B","C","D", "B","C","C","A","D", "C","C","C","D","A"}; String[] answersGiven2 = {"B","D","B","A","D", "A","B","B","C","D", "B","C","C","A","D", "E","C","C","D","A"}; DriverExam myAttempt1 = new DriverExam(answersGiven1); DriverExam myAttempt2 = new DriverExam(answersGiven2); displayResults(myAttempt1,"Attempt 1:"); displayResults(myAttempt2,"Attempt 2:"); } public static void displayResults(DriverExam attempt, String title) { System.out.println("\n" + title); System.out.println("Passed? " + attempt.passed()); System.out.println("# correct " + attempt.totalCorrect()); System.out.println("# missed " + attempt.totalIncorrect()); System.out.print("Questions missed: "); for (int missedQuestion : attempt.questionsMissed()) System.out.printf("%2d ", missedQuestion); System.out.println(""); } }
public class DriverExam { private String[] correctAnswers = {"B","D","B","A","C", "A","B","B","C","D", "B","C","C","A","D", "C","C","C","D","A"}; private String[] studentAnswers; public DriverExam(String[] newAnswers) { for(int i = 0; i < correctAnswers.length; i++) studentAnswers[i] = newAnswers[i]; } public boolean passed() { return (totalCorrect()>15); } public int totalCorrect() { int correctCount = 0; for (int i = 0; i < correctAnswers.length; i++) { if(correctAnswers[i].equals(studentAnswers[i])) correctCount++; } return correctCount; } public int totalIncorrect() { int totalMissed = 0; totalMissed = correctAnswers.length - totalCorrect(); return totalMissed; } public int[] questionsMissed() { int[] missedQuestion = new int [totalIncorrect()]; return missedQuestion; } }