hi

i have writtten a program which incorporrates pascals triangle to calcuate n choose r, but i have followed the task instructions and seem to get an outofbounds error and cant seem to make out whats wrong:

Code:
public class TestPascal {
   
    public static void main(String[] args) {
        InputFrame theInputFrame = new InputFrame();
        while(true){
            int n = theInputFrame.getInt();
            int r = theInputFrame.getInt();
            //int maxN = (n > 10) ? n - 10: 0;
            //PascalTriangle test = new PascalTriangle(maxN);
            PascalTriangle test = new PascalTriangle(n-10);
            if (n > 10){
            System.out.println(n+", "+r);
            System.out.println(test.binomial(n,r));
            }   
        }
    }
}
this is the code for the test program which gets values for n and r

this is the code that causes an error and i have highlighted the line:

Code:
 

public class PascalTriangle {
    private int maxN;
    private double[][] triangle;
    
    PascalTriangle(int maxN){
        
        this.maxN=maxN;
        
        triangle = new double[maxN+1][];
        
        for (int n = 0; n < triangle.length; n++) {
            triangle[n] = new double[n+1];
            
            for (int r = 0; r < triangle[n].length; r++) {
                triangle[n][r] = -1;
            }
            triangle[n][n] = 1;
        }
        
    }
    
    
    public double binomial(int n, int r) {
        
       
        if (triangle[n][r] >= 0) {//line that causes the error in the outofbounds
            return triangle[n][r];
        } else {
            return triangle[n][r] = binomial(n - 1, r - 1) + binomial(n - 1, r);
   }
       
    }
the binomial method is meant to do the follwoing:

The method body is the same basic algorithm, except that if 0 <= n <= maxN and 0 <= r <= n then triangle[n][r] must be taken into account:

--If triangle[n][r] is positive, then it is returned as the result.

--Otherwise the result is calculated by the usual recursion but is then also assigned to triangle[n][r].

this what i the output window does when i enter a value:

30, 15

java.lang.ArrayIndexOutOfBoundsException: 30
at ex9.PascalTriangle.binomial(PascalTriangle.java:50)//this line is highlighted above
at ex9.TestPascal.main(TestPascal.java:26)

any help would be great