my friend and I have the same exact code, but only mine shows up with a null line. We are both very new to coding, so we don't really know what to do, or how to fix it, especially since it seems to be working for her. Java is telling me that my
StringTokenizer st= new StringTokenizer(bf.readLine());
is null. I read online that I can use a try-catch method to run my program with the null line in it. However, my terminal window has to print out a certain way (for my teacher), so i cannot have the try-catch method leaving an extra print line when I run it. If anyone has any solutions, please let me know! Also, if someone could please explain how a code can have a null line for one person, and not the other would be great, because I really don't know why it is working for her, and not me.
Here is my full code, I indicated which line contains the null line.
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class QuizGrading
{
public static void main (String[]args) throws IOException
{
///key part
System.out.println(" ");
BufferedReader bf = new BufferedReader(new FileReader("key.in"));
StringTokenizer st = new StringTokenizer(bf.readLine()); **(NULL)**
int num = Integer.parseInt(st.nextToken());
int [] key = new int[num];
st=new StringTokenizer(bf.readLine());
for (int i=0; i<num; i++)
{
int an = Integer.parseInt(st.nextToken());
key[i] = an;
}
///answer part
BufferedReader br = new BufferedReader(new FileReader("tenQuizzes.in"));
StringTokenizer answers = new StringTokenizer(br.readLine());
int stu=1;
int numq = Integer.parseInt(answers.nextToken());
for (int u=0; u<numq; u++)
{
int right=0;
answers = new StringTokenizer(br.readLine());
for (int s=0; s<num; s++)
{
int ans=Integer.parseInt(answers.nextToken());
if (ans==key[s])
{
right=right+1;
}
}
double percent = right*100/num;
System.out.println("There are "+right+" questions correct.");
System.out.println("Student "+stu+" scored "+percent+"%.");
System.out.println(" ");
stu=stu+1;
}
System.out.println(" ");
}
}
BufferedReader.readLine()
returns null
if end of file is reached. Guess your are not reading the same file. The file causing the NPE may be empty after all.