java - 无法找出 java.lang.NullPointerException 错误

标签 java arrays string

所以,我几乎一整天都在研究这个项目,但我不明白为什么会出现这些错误。我知道这与将 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();
        }
    }

如果有帮助的话,这里是作业描述。 宾夕法尼亚学院要求您编写一个程序,对某项考试的笔试部分进行评分。该考试有 20 道多项选择题。以下是正确答案:

A、D、C、D、A、B、B、D、A、C、D、C、B、A、B、C、D、A、A、B

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

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

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

谢谢。

最佳答案

您的类(class)DemoTestGrader创建一个数组 setKey它自己的,但它实际上从未设置名为 setKey 的字段在类里面TestGrader .

您应该调整构造函数以接受 grade和一个 setkey :

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 ,或者直接删除它并使用上面的预设数组。

关于java - 无法找出 java.lang.NullPointerException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30112000/

相关文章:

java - 如何使静态日历线程安全

Ruby 中的 Java 匿名接口(interface)实现

c - 指向结构数组的指针

java - 如何在 Java 中打印特定元素之后的所有数组元素?

string - 在 MATLAB 中计算字符串单元格

python - 查找字符串中某个字符的所有出现

java - 如何在开关中随机定义多个变量?

C 读取文件并将数字保存在数组中

c++ - 发送输入字符串?

java - 如何使用 REST API 为每个用户创建不同的资源