This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                4年前关闭。
            
        

因此,我几乎整天都在从事这个项目,我无法弄清楚为什么会出现这些错误。我知道将setKey从DemoTestGrader传递到TestGrader是有原因的,但是就像我说的那样,我无法弄清楚。


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)



该程序包含两个文件,这些文件一起对测试进行评分。

程序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;
    }
}


程序2(主):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();
        }
    }


同样,如果有帮助,这里是作业说明。
宾夕法尼亚大学(Penn College)要求您编写一个程序,对某个考试的书面部分进行评分。这项考试有20个多项选择题。这是正确的答案:

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

为此,您应该创建一个TestGrader类。该课程将包含20个字符的答案数组,其中将包含正确的测试答案。它具有两个公共成员函数,这些函数使用户程序可以与该类进行交互:setKey和grade。 setKey函数接收包含正确答案的20个字符的字符串,并将此信息复制到其答案数组中。评分功能会接收一个包含20个字符的数组,其中包含应试者的答案,并将每个答案与正确答案进行比较。申请人必须正确回答20个问题中的15个或更多才能通过考试。在对考试进行“评分”后,评分功能应创建并向用户返回一个包含以下信息的字符串:


一条消息,指示申请人是否通过了考试
正确回答的问题总数,错误回答的问题总数


创建并使用TestGrader对象的客户端程序应首先对setKey进行一次调用,并将包含20个正确答案的字符串传递给它。完成此操作后,应允许输入考生的20个答案,并将其存储在20个字符的数组中,然后调用评分功能为考试评分。该程序应循环播放,以允许输入其他测试并对其进行评分,直到用户表明希望退出为止。

谢谢。

最佳答案

您的类DemoTestGrader创建自己的数组setKey,但实际上从未在类setKey中设置名为TestGrader的字段。

您应该调整构造函数以接受gradesetkey

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


我也不明白您的setKey循环是干什么的:

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;
    }


通过使循环条件为setKey来修复它以实际更新x < setKey.length,或者摆脱它并使用上面的预置数组。

08-05 07:48