Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
and it says that it is in the line:
Code:
int[] A = new int[questions.length];
This is the whole code. This is used to generate a set of questions randomly.
Code:
public void askQuestions(String[] questions, String[] answers) {
int count = 0;
int point = 0;
// Create the arrays
int[] A = new int[questions.length];
int[] B = new int[A.length];
// Create the counters
int countA = A.length;
int countB = 0;
// adding the indexes to A
for (int i = 0; i < A.length; i++){
A[i] = i;
}
while (countA > 0){
Random generator = new Random();
int pos = generator.nextInt(countA);
// Adding the index of A at pos to B
B[countB] = A[pos];
countB++;
// Removing the index of A at pos from A
for (int i = pos; i < A.length - 1; i++)
{
A[i] = A[i + 1];
}
countA--;
}
02-26-2008, 10:29 AM
nspils
sounds as if questions.length is not initialized with a value. Try testing for a value in your code - a debug statement before the creation of your A array to output the length of the questions array.
you do a lot of checking of the length of the array and use that number in many places - why not create a counter variable, initialize it once with the length of your string array, and then do all your A.length stuff using the counter value, rather than accessing the array again to get its length?
02-26-2008, 10:38 AM
mcal
Yeah ok. Thanks a lot. :)
02-26-2008, 10:31 PM
Peter_APIIT
Do u define it using #define statement or static ?