I'm working on a 2D array. The problem is really simple but I can't figure it out. When I try to access the value from index array[i-1][j]
it is throwing a Null Pointer exception. While it seems completely legitimate to me to access a index like array[2-1][2]
. Why it is not working? Is there any logic behind why I can't access array from one back index OR am I doing it wrong please explain.
Code to declare and initialize array:
this.Result = new Integer[len][6];
Code where Null pointer exception occurring:
for(int i=0;i<Result.length;i++) {
if(i==0){
Result[i][4]=0;
}
else if(Result[i][1]!=null) {
Result[i][4]=(Result[i-1][2]+Result[i-1][3])+Result[i][2];
}
}
Thanks in advance and please ignore grammar mistakes.