0

So, I've been working on this project just about all day and I cannot figure out why I am getting these errors. I know that it has something to due with passing setKey from DemoTestGrader to TestGrader, but like I said, I cannot figure it out.

Exception in thread "main" java.lang.NullPointerException
  at TestGrader.totalCorrect(TestGrader.java:19)
  at TestGrader.passed(TestGrader.java:14)
  at DemoTestGrader.main(DemoTestGrader.java:41)

The program consists of two files that together grade a test.

Program 1: TestGrader

// Chris Brocato
// 04-30-15
// This program will perform the methods for DemoTestGrader

public class TestGrader {
    private char[] setKey;
    private char[] grade;

    public TestGrader(char[] key){
        grade = key;
    }

    public boolean passed (){
        return (totalCorrect() > 14);
    }

    public int totalCorrect(){
        int correct = 0;
          for (int i = 0; i < setKey.length; i++){
              if (setKey[i] == grade[i])
                  correct++;
          }
         return correct;
    }
    public int totalMissed(){
        int tmissed = 0;
        tmissed = setKey.length - totalCorrect();
        return tmissed;
    }
    public int[] questionsMissed(){
        int size = setKey.length - totalCorrect();
        int[] missed = {};
        if (size < 1)
            return missed;
        else
            missed = new int [size];
        int pos = 0;
        for (int i = 0; i < setKey.length; i++){
            if (setKey[i] != grade[i]){
                missed[pos] = (i + 1);
                pos = pos + 1;
            }   
        }
        return missed;
    }
}  

Program 2(main): DemoTestGrader

// Chris Brocato
// 05-02-15
// This program will use TestGrader.java to grade a test of 20 questions

import java.util.Scanner;

public class DemoTestGrader {

    public static void main(String[] args) {

        // Declare variables and objects
        char[] setKey = {'A', 'D', 'C', 'D', 'A', 'B', 'B', 'D', 'A', 'C', 'D', 'C', 'B', 'A', 'B', 'C', 'D', 'A', 'A', 'B'};
        char[] grade = new char[20];
        Scanner keyScan = new Scanner(System.in);

        // 
        for(int x = 0; x == setKey.length; x++){
            char input;
            do{
                input = Character.toUpperCase(keyScan.next().charAt(0));
                }while(input < 'A' || input >'D');
            // store answer
            setKey[x] = input;
        }

        // Ask user for input and loop through and ask for an answer for each question
        System.out.println("Please enter the letter chosen for each answer: ");
        for(int i = 0; i < grade.length; i++){
            char input;
            do{
                System.out.print(i + 1 + ". ");
                input = Character.toUpperCase(keyScan.next().charAt(0));
                }while(input < 'A' || input >'D');
            // store answer
            grade[i] = input;
        }

        // Print the output to the screen
        TestGrader test = new TestGrader(grade);
        System.out.println();
        System.out.println("You " + (test.passed()?"passed" : "did not pass") + ".\n");
        System.out.println("Correct: " + test.totalCorrect() + "\n");
        System.out.println("Incorrect: " + test.totalMissed() + "\n");
        System.out.println("Questions missed: " + test.questionsMissed());

        // Close scanners
        keyScan.close();
        }
    }

Also if it helps, here is the assignment description. Penn College has asked you to write a program that grades the written portion of a certain exam. This exam has 20 multiple choice questions. Here are the correct answers:

A, D, C, D, A, B, B, D, A, C, D, C, B, A, B, C, D, A, A, B

To do this you should create a TestGrader class. The class will have an answers array of 20 characters, which will hold the correct test answers. It will have two public member functions that enable user programs to interact with the class: setKey and grade. The setKey function receives a 20-character string holding the correct answers, and copies this information into its answers array. The grade function receives a 20-character array holding the test taker’s answers and compares each of their answers to the correct one. An applicant must correctly answer 15 or more of the 20 questions to pass the exam. After “grading” the exam, the grade function should create and return to the user a string that includes the following information:

  • a message indicating whether the applicant passed or failed the exam
  • the total number of correctly answered questions, the total number of incorrectly answered questions

The client program that creates and uses a TestGrader object should first make a single call to setKey, passing it string containing the 20 correct answers. Once this is done it should allow a test taker’s 20 answers to be entered, store them in a 20-character array, and then call the grade function to grade the exam. The program should loop to allow additional tests to be entered and graded until the user indicates a desire to quit.

Thanks.

2 Answers2

1

Your class DemoTestGrader creates an array setKey of its own, but it never actually sets the field called setKey in the class TestGrader.

You should adjust the constructor to accept both a grade and a setkey:

public TestGrader(char[] grade_, char[] setKey_){
    grade = grade_;
    setKey = setKey_;
}

I also do not understand what your setKey loop is for:

for(int x = 0; x == setKey.length; x++){
        char input;
        do{
            input = Character.toUpperCase(keyScan.next().charAt(0));
            }while(input < 'A' || input >'D');
        // store answer
        setKey[x] = input;
    }

Either fix it to actually update setKey by making the loop condition x < setKey.length, or just get rid of it and use the pre-set array that you have above.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

Look at setKey here:

public class TestGrader {
    private char[] setKey;
    private char[] grade;

Where do you set it? Nowhere. So when you run:

public int totalCorrect(){
    int correct = 0;
      for (int i = 0; i < setKey.length; i++){
          if (setKey[i] == grade[i])
              correct++;
      }
     return correct;
}

You'll get a NullPointerException since setKey is null. Read the instructions again:

The client program that creates and uses a TestGrader object should first make a single call to setKey, passing it string containing the 20 correct answers.

It should be clear from that what you need to do.

martinez314
  • 12,162
  • 5
  • 36
  • 63