import java.math.BigInteger;
public class PascalsTriangle {
public int row = 0;
public BigInteger[][] pascal = new BigInteger[ (int) (row +1)][];
public PascalsTriangle(int dimension) {
row = dimension + 2;
pascal = new BigInteger[row +1][];
pascal[1] = new BigInteger[1 + 2];
pascal[1][1] = new BigInteger("1");
for (int i = 2; i <= row; i++) {
pascal[i] = new BigInteger[i + 2];
for (int j = 1; j < pascal[i].length - 1; j++) {
pascal[i][j] = pascal[i - 1][j - 1].add(pascal[i-1][j]) ;
}
}
}
public BigInteger getface(int row, int column) {
return pascal[row][column];
}
}
I was trying to print a pascals triangle but the integer requirement for me is very high, i.e more than the max value of long
, so I used BigInteger. But I'm getting a NullPointerException at the part where I add BigIntegers in the class PascalsTriangle. Is this the right way to add a 2 dimensional BigInteger array?