Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with two-dimensional array print method

Status
Not open for further replies.

laina222

Technical User
Sep 7, 2001
172
US
I'm trying to write a method to print each row of my two-dimensional array of doubles, called 'M', and I've written the following print method.

public void print()
{
for (i = 0;i < size;i++)
{
System.out.println();
for (j = 0;j < size;j++)
{
System.out.println(M[j] + &quot;&quot;);
}
}
}

The class compiles fine, but when I run the driver program I get a null pointer exception error, which points directly to the line that says &quot;System.out.println(M[j] + &quot;&quot;);&quot;. Any ideas on why that may be? At first someone suggested that I wasn't converting the double to a String, but I figured if I had (M[j] + &quot;&quot;), that would convert the double to a String. I also tried to do toString(M[j]), but got a completely different error when I compiled the class, so I knew that wouldn't work.
Any suggestions?
Thanks in advance!
 
The reference M[j] is incorrect. On a two-dimensional array you need to reference each double as M[j].

Also, use of &quot;size&quot; for both the number of rows in the array (which would be i) or the number of doubles in each row (which would be j) will only work if the number of rows and the number of doubles in each rows is the same. Just a freindly warning...
























&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
You guys can't use the letter i as an indices [ i ] because of TGML, unless you put it inside of a [ code ] block or turn off TGML (see the checkbox Process TGML)

The following is inside a [ code ]...[/ code ] block.
Code:
M[j][i]

-pete

 
I'm sorry - there's a typo in my original post. I meant to say the line I'm having trouble with is
System.out.println(M[j] + &quot;&quot;);
I did include the i and the j.
Oh, and &quot;size&quot; is a variable to indicate the number of rows and columns in the array, which are always set to the same value.
Sorry about the confusion!
 
If tek-tips does this to me one more time, I'll scream.
The line is supposed to be M[ i ][j] - I did include the i and the j in the original post, but the &quot;[ i ]&quot; keeps telling tek-tips to italicize everything.
 
Can you post the code where your array is declared and initialized?

(My new solution is to italicize everything so I'll remember!.)

&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
Of course! I hope this helps...

private double [][] M;
private final int MAX_SIZE = 100;
private int size;
private int i, j;

//default constructor
public Matrix (int N)
{
if (N < 0)
{
N = 0;
}
else if (N > 0 && N <= MAX_SIZE)
{
N = N;
}
else
N = MAX_SIZE;

size = N;

double [][] M = new double [N][N];

//loop to fill values of array with zeroes
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
M[ i ][j] = 0;
}
}
}//end of default constructor

//constructor
Matrix(int N, double[][] initValues)
{
if (N < 0)
{
N = 0;
}
else if (N > 0 && N <= MAX_SIZE)
{
N = N;
}
else
N = MAX_SIZE;

size = N;

double [][] M = new double [N][N];

//loop to fill values of array 'M' with same values from 'initValues'
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
M[ i ][j] = initValues[ i ][j];
}
}
}//end of constructor


Those are the two constructors I have to fill the arrays. The first one fills it with zeros. The print method comes right after these two.
And if this whole post is in italics, then it's because I didn't put spaces around the 'i' in the brackets!
Thanks!
 
>> If tek-tips does this to me one more time, I'll scream.

ummm... i already addressed that issue in my post. Did you even read it?

-pete

 
Code:
for(int nRow=0; nRow<M.length; nRow++){
	for(int nCol=0; nCol<M[nRow].length; nCol++)
		System.out.println(nRow + &quot;,&quot; + nCol + &quot;: &quot; + M[nRow][nCol]);
}
-pete

 
I dunno ... on the code line

Code:
      System.out.println(M[i][j] + &quot;&quot;);

there's only one object (M), so there's only one source of the null pointer exception, which is that M=null. Given that, you may have a scope issue here. Is M defined anywhere else also? Is print() in the same class as M?

&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
idarke - print() is in the same class as M, and initValues is set with values in the driver class.

palbano - I read your post after I re-posted my code, sorry.
 
You know, your second constructor isn't actually a constructor. It needs the public keyword on it.

So, when you create Matrix, you may not be calling a constructor at all. That would explain why M is null.

Put some System.out.println() debug messages at the start of every method, including the constructors, and see what's actually being called...

&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
Please ignore previous post. I typed without thinking.

I've put your two code posts together into a small app that fails with a nullPointerException. More in a few minutes...

&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
OK. The line

Code:
                           double [][] M = new double [N][N];

in your constructor is creating a local array with the same name as the global one, thereby hiding the global one. The constructor is initializing the local array, and the global one is never initialized. Change that line to:

Code:
                            M = new double [N][N];

Sorry it took so long...

&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
double [][] M = new double [N][N];

//loop to fill values of array with zeroes
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
M[ i ][j] = 0;
}
}


Java initializes all your elements to 0.0 so

Code:
double M[][] = new double[N][N];

is all you need

-pete

 
Thanks for the help! I finally got it working
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top