I have written the following method that shows you the number of correct, incorrect and blank answers you have for an exam.
The Problem is that overtime I try to compile the file, I get a Null Pointer Exception
I have tried moving around the variables and reinitializing them, but to no avail.
How can I fix the problem. (Sorry about that, I swear to god this is the full code:)
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();
}
}
This is what the console gave me at the time of execution:
Exception in thread "main" java.lang.NullPointerException
at Exam.Analysis2(Exam.java:93)
at Exam.main(Exam.java:116)
br.readLine()
and don't check if the returned String is null. I added a null check and it ran without any errors. – redxef Aug 01 '15 at 13:57