我正在嘗試將Pascal三角形打印成一百行,但Java的int似乎返回負值。我正在嘗試使用BigInteger,但每次添加兩個數字時都會得到NullPointerException!我想我已經初始化了它們。這裏是我的代碼:爲什麼我的BigInteger.add()會得到NullPointerException?
import java.math.BigInteger;
public class Pascal {
public static void main(String[] args) {
BigInteger[][] p = new BigInteger[100][];
p[0] = new BigInteger[3];
p[0][1] = BigInteger.ONE;
for (int i = 1; i <= N; i++) {
p[i] = new BigInteger[i + 3];
for(int j = 0; j < p[i].length; j++){
p[i][j] = new BigInteger("0");
}
for (int j = 1; j < p[i].length - 1; j++)
p[i][j] = p[i-1][j-1].add(p[i-1][j]); //NPE!
}
for (int i = 0; i <= N; i++) {
for (int j = 1; j < p[i].length - 1; j++) {
System.out.print(p[i][j] + " ");
}
System.out.println();
}
}
}