Home arrow static arrow Java Programming [Archive] - Problems with my matrix...
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Problems with my matrix...
This topic has 15 replies on 2 pages.    1 | 2 | Next »

Posts:14
Registered: 6/25/04
Problems with my matrix...  
Jun 25, 2004 12:07 PM



 
Hi,

I get this error when I try to run through this part of my code:
"java.lang.NullPointerException"
What am I doing wrong?

    int counter1=0, counter2=0, sum=0;    Double temp1, temp2, result;    Double[] Array1, Array2;    Double[][] newMatrix, dmatrix;.........            while(counter1 < dmatrix.length) {            for(int i = 0; i < dmatrix[counter1].length; i++) {                Array1[i] = dmatrix[counter1][i]; // <--I believe the errors are here?  But it could be due to something else                System.out.println(Array1[i]);            }            counter2 = counter1 + 1;            while(counter2 < dmatrix.length) {                for(int j = 0; j < dmatrix[counter2].length; j++) {                    Array2[j] = dmatrix[counter2][j]; // <--I believe the errors are here?  But it could be due to something else                }            }            for(int k = 0; k < Array2.length; k++) {                temp1 = Array1[k];                temp2 = Array2[k];                                if(temp1.compareTo(temp2) < 0) {                    result = new Double (result.doubleValue() + ( temp1.doubleValue()/temp2.doubleValue() ) );                 }                else if(temp1.compareTo(temp2) > 0) {                    result = new Double (result.doubleValue() + ( temp2.doubleValue()/temp1.doubleValue() ) );                 }                sum++;            }            System.out.println(result);            counter1++;            sum = 0;        }
 

Posts:2,206
Registered: 8/15/02
Re: Problems with my matrix...  
Jun 25, 2004 12:08 PM (reply 1 of 15)



 
Where do you set up dmatrix - allocate space for it I mean
 

Posts:121
Registered: 12/16/03
Re: Problems with my matrix...  
Jun 25, 2004 12:16 PM (reply 2 of 15)



 
dmatrix.length - 1


you may need to minus one as the loop might be goign out of the array
 

Posts:21,719
Registered: 98-02-20
Re: Problems with my matrix...  
Jun 25, 2004 12:19 PM (reply 3 of 15)



 
Where do you allocate memory for these?

Double[] Array1, Array2;    Double[][] newMatrix, dmatrix;


Your biggest mistake is not turning on line number display in your text editor and going to the line number where the NPE was thrown to figure out why this happened.

%
 

Posts:21,719
Registered: 98-02-20
Re: Problems with my matrix...  
Jun 25, 2004 12:20 PM (reply 4 of 15)



 
dmatrix.length - 1


you may need to minus one as the loop might be goign
out of the array

He didn't get an IndexOutOfBoundsException, it's a NPE.

%
 

Posts:14
Registered: 6/25/04
Re: Problems with my matrix...  
Jun 25, 2004 12:23 PM (reply 5 of 15)



 
No, dmatrix is fine, I think it it in the way I am assing the variables
            Array1[i] = dmatrix[counter1][i];


Do I need instantiation?
 

Posts:14
Registered: 6/25/04
Re: Problems with my matrix...  
Jun 25, 2004 12:24 PM (reply 6 of 15)



 
Where do you allocate memory for these?

Double[] Array1, Array2;Double[][] newMatrix, dmatrix;


Your biggest mistake is not turning on line number
display in your text editor and going to the line
number where the NPE was thrown to figure out why this
happened.

%

The line is where I assign the array a value from dmatrix...:

                  Array1[i] = dmatrix[counter1][i];
 

Posts:2,206
Registered: 8/15/02
Re: Problems with my matrix...  
Jun 25, 2004 12:25 PM (reply 7 of 15)



 
Umm, yes.
 

Posts:14
Registered: 6/25/04
Re: Problems with my matrix...  
Jun 25, 2004 12:26 PM (reply 8 of 15)



 
How do I go about instantiating this?
 

Posts:21,719
Registered: 98-02-20
Re: Problems with my matrix...  
Jun 25, 2004 12:26 PM (reply 9 of 15)



 
Double[] Array1, Array2;Double[][] newMatrix, dmatrix;


Wrong. You haven't allocated any memory. Where's the call to new? All you've done is declare variables of a given type.

You need to do something more like this:

public class MatrixTester{    public static void main(String [] args)    {        int numRows = 5;        int numCols = 5;        double [][] matrix = new double[numRows][];        for (int m = 0; m < numRows; ++m)        {            matrix[m] = new double[numCols];            for (int n = 0; n < numCols; ++n)            {                matrix[m][n] = (double)(m + n);            }        }         for (int m = 0; m < numRows; ++m)        {            for (int n = 0; n < numCols; ++n)            {                System.out.print(matrix[m][n] + " ");            }            System.out.println();        }    }}


Note the calls to "new".

%
 

Posts:2,206
Registered: 8/15/02
Re: Problems with my matrix...  
Jun 25, 2004 12:28 PM (reply 10 of 15)



 
Thanks duffy
 

Posts:14
Registered: 6/25/04
Re: Problems with my matrix...  
Jun 25, 2004 12:48 PM (reply 11 of 15)



 
The problem isn't with my matrix sizing...I will be reading my matrix in from a csv file, and then allocating it. That all works fine. Its when I start to do the math on the matrix variables. Thats when I hit a pole. Perhaps I need to make new Double on my Array1?
            for(int i = 0; i < dmatrix[counter1].length-1; i++) {                Array1[i] = dmatrix[counter1][i];  // <-- This is the problem...                System.out.println(Array1[i]);            }


Perhaps making it like this?

Double[] Array1 = new Double[dmatrix.length];
 

Posts:2,206
Registered: 8/15/02
Re: Problems with my matrix...  
Jun 25, 2004 12:57 PM (reply 12 of 15)



 
Umm yes (again)
 

Posts:21,719
Registered: 98-02-20
Re: Problems with my matrix...  
Jun 25, 2004 12:59 PM (reply 13 of 15)



 
The problem isn't with my matrix sizing...I will be
reading my matrix in from a csv file, and then
allocating it. That all works fine.

Really? I figure that you'll have to allocate memory before you read anything into it.

It's possible to read a .csv file, tokenizing each line and adding a row for every line. But before you load any of that into your matrix you've got to allocate memory to hold those values.

You might have done this properly, but since I can't see your code I can't be sure.

My advice would be to check your assumptions. People cause themselves all kinds of pain by telling themselves "That all works fine". Perhaps it doesn't. You can check it by simply reading in that .csv into the matrix and then just printing it out. If you can't do that, you can't do any math operations.

Double[] Array1 = new Double[dmatrix.length];

If reading in the matrix doesn't initialize those arrays, I'd say yes. Wherever you do it, you have to allocate memory before you do something with an object. Until you do, Array1 points to null, and when you dereference it you'll get a NPE.

%
 

Posts:21,719
Registered: 98-02-20
Re: Problems with my matrix...  
Jun 25, 2004 1:00 PM (reply 14 of 15)



 
EvilEdna has given you the right answer twice in few words. Start thinking more about how memory works.

%
 
This topic has 15 replies on 2 pages.    1 | 2 | Next »