logo
Published on Computer Science Foundations (http://www2.evergreen.edu/csf)

drjava output errors

I'm having this weird problem with my Pascal Triangle program....the program compiles fine but when i run it and enter the number of rows the interactions pane gives me this:

 

NullPointerException:
at PascalTri.displayRows(PascalTri.java:30)
at PascalDriver.main(PascalDriver.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)

 

Here's my code if this helps:

-----------------------

import java.util.Scanner;

public class PascalDriver{
public static void main (String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of rows");
int rowCount = scan.nextInt();
PascalTri t = new PascalTri(rowCount);
t.displayRows();
}
}

 

-------------------

public class PascalTri{
private int rows;
//creates PascalTri object
public PascalTri(int x){
rows = x;
}
//takes input from Pascal Tri object and turns it into a pascal triangle display!
public void displayRows(){
long[][] pascalArray = new long[rows][];

for(int i=0; i<=rows;i++){
pascalArray[i] = new long[i+1];

//first element of each row is 1
pascalArray[i][0] = 1;

for(int j=1; j<i; j++) {
pascalArray[i][j] = pascalArray[i-1][j-1] + pascalArray[i-1][j];
}

//last element of each row is 1 too
pascalArray[i][i] = 1;

//loop through and display the 2 dimensional array

for (int k=0; k<rows; k++) {
for (int j=0; j<=k; j++) {

System.out.print(pascalArray[k][j]+" ");

}
// carrige return
System.out.println("");
}

}

}//end method

}//end class

Owl › [0]

Source URL:
http://www2.evergreen.edu/csf/csf/drjava-output-errors