NullPointException error

Hello, I am trying to run this program and I keep getting a NullPointException at line 11. I have no idea why. Thanks for the help.

class Fibonacci{

/**Print out the first few Fibonacci

numbers, marking evens with a '*' */

staticfinalint MAX_INDEX = 10;

publicstaticvoid main(String[] args){

int lo = 1;

int hi = 1;

FibNums[] array =new FibNums[MAX_INDEX];

array[0].number = 1;

array[0].mark =false;

for(int i = 2; i < MAX_INDEX; i++){

if (hi % 2 == 0)

array[i].mark =true;

else

array[i].mark =false;

array[i].number = hi;

hi = lo + hi;// new hi

/* new lo is (sum - old lo) i.e., the old hi */

lo = hi - lo;

}

for(int i = 0; i < array.length; i++)

System.out.println(i +": " + array[i].number + array[i].mark);

}

}

class FibNums{

publicint number;

publicboolean mark;

}

[2085 byte] By [Mag748a] at [2007-11-26 18:27:40]
# 1
> FibNums[] array = new FibNums[MAX_INDEX];The array is created here, but each element is null.> array[0].number = 1;Boom. You never created FibNums OBJECTS in that array, so array[0] is null, so you're trying to invoke null.number --> NullPointerException.
warnerjaa at 2007-7-9 6:01:49 > top of Java-index,Java Essentials,New To Java...
# 2

ok, so then would I have to have a for loop right after the array declaration like this:

for(int i = 0; i < array.length; i++)

array[i] = new FibNums();

is there a better way to do that?

Thank you!

Mag748a at 2007-7-9 6:01:49 > top of Java-index,Java Essentials,New To Java...
# 3
> is there a better way to do that?No, that's the way.
warnerjaa at 2007-7-9 6:01:49 > top of Java-index,Java Essentials,New To Java...
# 4
Alrighty. Thanks.
Mag748a at 2007-7-9 6:01:49 > top of Java-index,Java Essentials,New To Java...
# 5

Quoting from your code:

for(int i = 2; i < MAX_INDEX; i++) {

if (hi % 2 == 0)

array[i].mark = true;

else

array[i].mark = false;

array[i].number = hi;

hi = lo + hi;// new hi

/* new lo is (sum - old lo) i.e., the old hi */

lo = hi - lo;

}

1) Why is the loop starting at 2? You set the item at index 0, but what about 1?

2) The indenting of your "else" block makes it look like you think those statements are part of the "else", but since you didn't actually block them with { }, they aren't. The compiler doesn't "see" the indentation.

warnerjaa at 2007-7-9 6:01:49 > top of Java-index,Java Essentials,New To Java...
# 6

Yes, those are good questions. I had just copied an example from a book, and then it asked me to modify the program to include an array and a class. I hadn't finished the modification yet. Here is the completed program. Thanks!

class Fibonacci {

/**Print out the first few Fibonacci

numbers, marking evens with a '*' */

static final int MAX_INDEX = 10;

public static void main(String[] args) {

int lo = 1;

int hi = 1;

FibNums[] array = new FibNums[MAX_INDEX];

for(int i = 0; i < array.length; i++)

array[i] = new FibNums();

array[0].number = 1;

array[0].mark = false;

for(int i = 1; i < MAX_INDEX; i++) {

if (hi % 2 == 0)

array[i].mark = true;

else

array[i].mark = false;

array[i].number = hi;

hi = lo + hi;// new hi

/* new lo is (sum - old lo) i.e., the old hi */

lo = hi - lo;

}

for(int i = 0; i < array.length; i++){

System.out.print((i+1) + ": " + array[i].number);

if(array[i].mark)

System.out.println(" *");

else

System.out.println("");

}

}

}

class FibNums {

public int number;

public boolean mark;

}

Message was edited by:

Mag748

Mag748a at 2007-7-9 6:01:49 > top of Java-index,Java Essentials,New To Java...