[英]Null Pointer Exception (Java) [EDIT!]
我编写了以下方法,向您显示考试的正确、错误和空白答案的数量。 问题是加班我尝试编译文件,我得到一个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();
}
}
这是控制台在执行时给我的:
线程“main”中的异常 java.lang.NullPointerException
在 Exam.Analysis2(Exam.java:93)
在 Exam.main(Exam.java:116)
你应该在 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++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.