空指针异常(Java)[编辑!]

时间:2015-08-01 13:25:20

标签: java

我写了以下方法,向您显示考试中正确,错误和空白答案的数量。 问题是加班我尝试编译文件,我得到Null Pointer Exception我试图移动变量并重新初始化它们,但无济于事。

如何解决问题。 (对不起,我向上帝发誓这是完整的代码:)

import java.io.*;
import java.util.Scanner;
import java.lang.*;

public class Exam
{

  public static int numOfQ; //Number of Questions
  public static int numOfS; //Number of Answers
  public static String answers; //Correct Answers
  public static String answersFile; //Student Answers File
  public static BufferedReader br;

  public static void Start()
  {
    numOfQ = 10;
    System.out.println();
    System.out.println("Welcome to Exam Analysis.  Let’s begin ...");

    System.out.println();
    System.out.println();

    System.out.println("Please type the correct answers to the exam questions,");
    System.out.print("one right after the other: ");
    Scanner scan = new Scanner(System.in);
    answers = scan.nextLine();

    System.out.println("What is the name of the file containing each student's");
    System.out.print("responses to the " + numOfQ + " questions? ");
    answersFile = scan.nextLine();
    System.out.println();
  }

  public static void Responses() throws FileNotFoundException, IOException
  {
    br = new BufferedReader(new FileReader(answersFile));
    String line = null;
    while ((line = br.readLine()) != null)
    {
      numOfS++;
      System.out.println("Student #" + numOfS+ "\'s responses: " + line);
    }
    System.out.println("We have reached “end of file!”");
    System.out.println();
    System.out.println("Thank you for the data on " + numOfS + " students. Here is the analysis:");
  }

  public static void Analysis1() throws FileNotFoundException, IOException
  {
    System.out.println("Student #        Correct        Incorrect       Blank");
    System.out.println("~~~~~~~~~        ~~~~~~~        ~~~~~~~~~       ~~~~~");
    int i = 0;
    for (int start = 0; start < numOfS; start++)
    {
      i++;

      int correct = 0;
      int incorrect = 0;
      int blank = 0;

      if (br.readLine().charAt(start) == (answers.charAt(start)))
      {
        correct++;
      }
      else {
        if (br.readLine().charAt(start) == ' ')
        {
          blank++;
        }
        else
        {
          incorrect++;
        }
        System.out.printf("%2d  %2d  %2d %2d", i, correct, incorrect, blank);
      }
    }
  }

  public static void Analysis2() throws FileNotFoundException, IOException
  {
    System.out.println("QUESTION ANALYSIS         (* marks the correct response)");
    System.out.println("~~~~~~~~~~~~~~~~~");
    for (int i = 1; i <= numOfQ; i++ )
    {
      int a = 0;
      int b = 0;
      int c = 0;
      int d = 0;
      int e = 0;
      System.out.println("Question #" + i + ":");
      if (br.readLine().charAt(i - 1) == 'A')
      {
        a++;
      }
      else if (br.readLine().charAt(i - 1) == 'B')
      {
          b++;
      } else if (br.readLine().charAt(i - 1) == 'C')
      {
        c++;
      } else if (br.readLine().charAt(i - 1) == 'D')
      {
        d++;
      } else if (br.readLine().charAt(i - 1) == 'E')
      {
        e++;
      }
    }
  }

  public static void main(String[] args) throws FileNotFoundException, IOException {
    Start();
    Responses();
    Analysis1();
  }
}

这是控制台在执行时给我的东西:

  

线程中的异常&#34; main&#34;显示java.lang.NullPointerException

     

在Exam.Analysis2(Exam.java:93)

     

在Exam.main(Exam.java:116)

1 个答案:

答案 0 :(得分:1)

你应该在Analysis2的for循环中做什么,为br.readLine()。charAt(i-1)创建一个字符变量,然后检查不同的字符。为什么你会在for循环中进行int初始化?

 int a = 0;
  int b = 0;
  int c = 0;
  int d = 0;
  int e = 0;
 for (int i = 1; i <= numOfQ; i++ )
{

  System.out.println("Question #" + i + ":");

  char ans = br.readLine().charAt(i - 1);

  if (ans == 'A')
  {
    a++;
  }
  else if (ans == 'B')
  {
      b++;
  } else if (ans == 'C')
  {
    c++;
  } else if (ans == 'D')
  {
    d++;
  } else if (ans == 'E')
  {
    e++;
  }
}