-1

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)

Community
  • 1
  • 1
Jack Stew
  • 57
  • 8
  • 2
    Post enough code so we can reproduce. – Jean-François Savard Aug 01 '15 at 13:27
  • 1
    Post the whole error please. – Kevin Aug 01 '15 at 13:28
  • 1
    `br` can be `null`, `readLine()` can return `null`, `answers` can be `null`. I suggest that you [take a look at the stack trace and identify the line of code where it happens](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). Also you should use curly braces for your if/else statements, that would improve readability a lot. – Alexis C. Aug 01 '15 at 13:28
  • 1
    The fact that you read twice within the loop, seems suspicious to me. – Ronald Aug 01 '15 at 13:30
  • 3
    post full code and stack trace. – Karthik Aug 01 '15 at 13:30
  • you are calling `br.readline()` twice in one loop (in case the answer is not correct). might this be the issue? – Nir Levy Aug 01 '15 at 13:30
  • We still don't have the exception stacktrace. – Jean-François Savard Aug 01 '15 at 13:47
  • @JackStew I still don't see the full error... – Kevin Aug 01 '15 at 13:48
  • I tried compiling your code and it seems, that in your Analysis1 function you call 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
  • @redxef can you elaborate more please? – Jack Stew Aug 01 '15 at 14:00
  • @HyperZ I edit the code again. Can you check please. – Jack Stew Aug 01 '15 at 14:00
  • @Jean-FrançoisSavard I edit the code again. Can you check please. – Jack Stew Aug 01 '15 at 14:01
  • @JackStew: as others already mentioned - please show the exception stacktrace... and / or use a debugger. – home Aug 01 '15 at 14:04
  • @home I don't know what a stack trace is but if you mean the console, I mentioned that now. Can you please help me. I am really stuck – Jack Stew Aug 01 '15 at 14:11
  • So the problem is here: `at Exam.Analysis2(Exam.java:93)` (which line is it?). And now you show use your debuggers' force :-) – home Aug 01 '15 at 14:14
  • @home how can I debug this? I don't even know what the bug is :( Also the same bug is in Analysis1 – Jack Stew Aug 01 '15 at 14:16
  • @JackStew: use an IDE to debug (e.g. eclipse, netbeans). In case you just use an editor, add some debug statements (system.err.println). You try to access an object reference which is still null, so the line number is an indicator – home Aug 01 '15 at 14:18
  • @home can you write the command I have to use? Where do I have to write `system.err.println`? – Jack Stew Aug 01 '15 at 14:20
  • btw: `br.readLine()` always moves the cursor to the next line. So your `if` statements in `Axalysis2` `for` loop always read 5 lines. Your `br` might even be empty as you scrolled through all lines in `Responses` method. Read about the way readers work... – home Aug 01 '15 at 14:22

1 Answers1

1

What you should do in the for loop in Analysis2 is make a character variable for br.readLine().charAt(i-1) and then checking that up for the different characters. And why would you have the int initializations inside the for-loop?

 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++;
  }
}
Woksin
  • 74
  • 2
  • 9