我从下面第 19 行的代码中得到一个空指针异常,seq[index].value = lo;
. 序列类有一个整数类型的值字段,但我认为问题是访问 seq 实例的数组。
public class ImprovedFibonacci {
class Sequence{
int value = 0;
boolean isEven = false;
}
public static void main(String[] args){
final int MAX_LOOP = 20;
int lo = 1;
int hi = 1;
int i = 0;
String mark = "-";
int index = 0;
ImprovedFibonacci.Sequence[] seq = new ImprovedFibonacci.Sequence[MAX_LOOP];
seq[index].value = lo;
System.out.println("Fibonacci seq 1 : " + lo);
System.out.println("Sequence class index: "+index+"value: "+seq[index].value);
for(i=MAX_LOOP;i>=1;i--) {
hi = hi + lo;
lo = hi - lo;
index++;
if(hi % 2 == 0){
mark = "-";
seq[index].isEven = true;
}else{
mark = "";
}
System.out.println(i + " : " + hi + mark);
seq[index].value = hi;
System.out.println("Sequence class index: "+index+"value: "+seq[index].value+"IsEven: "+seq[index].isEven);
}
}
}