0
试图访问三角形阵列时空指针异常我试图访问我的三角阵列分别使用一种方法,每次我尝试使用tarray[i][j]
我得到一个空指针异常,除非它是在创建类,例如我有一个get方法,并使用return tarray[0][0]
,它只是抛出我的错误,即使它在创建内打印出来的罚款。当试图访问方法
我知道我可能做一些愚蠢的,但我无法弄清楚,
public class Triangular<A> implements Cloneable
{
private int inRa;
private A [][] tarray;
/**
* Constructor for objects of class Triangular
* @param indexRange - indices between 0 and indexRange-1 will be legal to index
* this a triangular array
* @throws IllegalArgumentException - if indexRange is negative
*/
public Triangular(int indexRange) throws IllegalArgumentException
{
inRa=indexRange;
int n = inRa;
int fill = 1;
Object [][] tarray = new Object [inRa + 1][];
for (int i = 0; i <= inRa; i++){
tarray[i] = new Object [n];
}
for (int i = 0; i < tarray.length; i++){
for (int j = 0; j + i < tarray[i].length; j++){
tarray[i][j + i] = fill;
fill ++;
}
}
for (int i = 0; i < tarray.length; i++) {
for (int j = 0; j + i < tarray[i].length; j++){
System.out.print(tarray[i][j + i] + " ");
}
System.out.println();
}
}
}
感谢您的帮助!
谢谢!这是固定的,是的,我应该已经意识到,我正在初始化它作为一个新的变量 – Identicon