array value assignment

hi,

I have the following code:

Uncomment the line (and comment the above) in getSubDetArray(), to see what is expected to get.

import java.math.BigDecimal;

publicclass SubDet{

publicstaticvoid main(String[] args){

BigDecimal[][] result =new BigDecimal[2][2];

BigDecimal[][] bd ={{new BigDecimal("1"),new BigDecimal("2"),new BigDecimal("3")},

{new BigDecimal("4"),new BigDecimal("1"),new BigDecimal("0")},

{new BigDecimal("2"),new BigDecimal("-3"),new BigDecimal("1")}};

for (int i = 0; i < bd.length; i++){

for (int j = 0; j < bd.length; j++){

System.out.print(bd[i][j].toString()+" ");

}

System.out.println();

}

result = getSubDetArray(bd, 0, 1);

}

//returns the sub-determinant array belonging to the specified row-column (m,n) index pairs

privatestatic BigDecimal[][] getSubDetArray(BigDecimal[][] data,int m,int n){

System.out.println("The sub-determinant at "+m+", "+n+":");

int r = data.length;

BigDecimal[][] subDet =new BigDecimal[r-1][r-1];

int k=0;

int l=0;

for (int i=0; i<r; i++){

if (i != m){

for (int j=0; j><r; j++){

if (j != n){

subDet[k][l] = data[i][j];

l++;

System.out.print(subDet[k][l].toPlainString()+" ");

//System.out.print(data[i][j].toPlainString()+" ");

}

}

l=0;

k++;

System.out.println();

}

}

return subDet;

}

}

Why am I not able to assign the data[i][j]

value to subDet[k][l]

?

thx,

emankcin>

[3913 byte] By [emankcina] at [2007-11-26 21:38:28]
# 1
> Why am I not able to assign the> data[i][j] value to> subDet[k][l] ?What happens when you try?Compile time error?Runtime error?Unexpected behavior?Physically prevented from typing that sequence of characters?
jverda at 2007-7-10 3:21:29 > top of Java-index,Java Essentials,Java Programming...
# 2

I get NPE at the line of the printout statement. The null "value" of the array never gets overwritten after its declaration...

Exception in thread "main" java.lang.NullPointerException

at SubDet.getSubDetArray(SubDet.java:37)

at SubDet.main(SubDet.java:20)

emankcin

emankcina at 2007-7-10 3:21:29 > top of Java-index,Java Essentials,Java Programming...
# 3
Which line is 37 of SubDet?
jverda at 2007-7-10 3:21:29 > top of Java-index,Java Essentials,Java Programming...
# 4
at me:System.out.print(subDet[k][l].toPlainString()+" ");emankcin
emankcina at 2007-7-10 3:21:29 > top of Java-index,Java Essentials,Java Programming...
# 5

> at me:

> > System.out.print(subDet[k][l].toPlainString()+" ");

>

>

> emankcin

Either subDet is null, or subDet[k] is, or subDet[k][ l ] is.

Use a debugger or print out each of those in turn to find out which one, then work backwards to figure out why.

jverda at 2007-7-10 3:21:29 > top of Java-index,Java Essentials,Java Programming...
# 6
At a guess, the l++ is causing you some problems.Also, l is a really horrible variable name. Looks to much like a number 1 or captial I.
jverda at 2007-7-10 3:21:29 > top of Java-index,Java Essentials,Java Programming...
# 7
> At a guess, the l++ is causing you some problems.>> Also, l is a really horrible variable name. Looks to> much like a number 1 or captial I.jverd, thx for the tips!
emankcina at 2007-7-10 3:21:29 > top of Java-index,Java Essentials,Java Programming...