I'm trying to do a q & a program which will read a question, 4 choices. Then read the answer and compare it to the correct answer. Then increments the player score,
My problem is, I always get null when I run the program. Could you please help me determine what's wrong with my program. I also get java.lang.nullexception and index array out of bounds.

File structure looks like this:
q
a
b
c
d
ans

where q is the question a,b,c,d are the choices. And the ans is the correct answer which will be compared to the players answer.

public static String[] q=new String[50];
public static String[] a=new String[50];
public static String[] b=new String[50];
public static String[] c=new String[50];
public static String[] d=new String[50];
public static char[] ans=new char[50];

try {


             FileReader fr;
      fr = new FileReader (new File("F:\\qa.txt"));
      BufferedReader br = new BufferedReader (fr);

      while (br.readLine()!= null) {
for(int ox=0;ox<5;ox++){

    q[ox]= new String();
    a[ox]= new String();
     b[ox]= new String();
     c[ox]= new String();
     d[ox]= new String();



         q[ox] = br.readLine();
         a[ox]=br.readLine();
         b[ox]=br.readLine();
         c[ox]=br.readLine();
         d[ox]=br.readLine();
         String strtemp=br.readLine();
         ans[ox]=strtemp.charAt(0);

}
      }
      br.close();

    } catch (Exception e) { e.printStackTrace();}


         for(int yy=0;yy<90;yy++){
          int ran= random(1,70);
          System.out.println(q[ran] + "\n" + a[ran] + "\n" + b[ran] + "\n" + c[ran] + "\n" + d[ran] + "\n" + "\n" + "Answer: ");
String strans=x.nextLine();
char y=strans.charAt(0);
if(y==ans[ran]){
    System.out.println("check!");
score++;
System.out.println("Score:" + score);
}else{
System.out.println("Wrong!");
}
    }

Here's the code that I use for randomizing:

public  int random(int min, int max){
    int xx;
    xx= (int) ( Math.random() * (max-min + 1))+ min;
    return xx;
    }

I always get null when I run the program

Please copy and paste here the full text of all error messages.

Please copy and paste here the full text of all error messages.

java.lang.ArrayIndexOutOfBoundsException: 56
java.lang.ArrayIndexOutOfBoundsException: 56
java.lang.NullPointerException

java.lang.NullPointerException

at quizzer.Main.play_mode(Main.java:212)
at quizzer.Main.Menu(Main.java:52)
at quizzer.Main.main(Main.java:27)
java.lang.ArrayIndexOutOfBoundsException: 52

Please copy and paste here the full text of all error messages.

You have edited out the source line numbers for the NPE error messages.
Without the full text of the error messages and the full text of your program showing the lines where the errors occurred, there is nothing that can be recommended.

Look at the source code at the line number in the error message and see what variable at that line can be null.

You have edited out the source line numbers for the NPE error messages.
Without the full text of the error messages and the full text of your program showing the lines where the errors occurred, there is nothing that can be recommended.

Look at the source code at the line number in the error message and see what variable at that line can be null.

I'm using netbeans what do I do so that I see the full text of error messages?

Sorry, I don't know anything about your IDE.

Can you use the java command to execute the program?

Edited 6 Years Ago by NormR1: n/a

Sorry, I don't know anything about your IDE.

Can you use the java command to execute the program?

You mean in the command line?Using java compiler?
Here it is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 54
at xd.main(xd.java:53)

Look at line 53 in the xd class and see what array is referenced there.
The code tried to use an index with value of 54 which is past the end of the array.
Check your code to see how the index gets past the end of the array.
The largest index for an array with 50 elements is 49.
What values can the variable ran have?

This article has been dead for over six months. Start a new discussion instead.