An easy one but where am I going wrong

S

sunstormrider

Dear group

This is driving me nuts - I thought I had it working and then not.

I have 2 classes - "tester" and "support"

support has two instance variables:

private int[] x
private int[] y

and a constructor something like this:

public support(int[] c, int[] d)
{
for (int i=0; i<x.length; i++)
{
x = c;
}
for (int i=0; i<y.length; i++)
{
y = d;
}
}

and support compiles fine.

Trouble is, when I come to pass the arguments to support from tester I
get a NullPointerException

E.g. in tester I have
int[] j = {1,2,3,4,5,6};
int[] k = {0,1,2,3,4,5};
support fred = new support(j,k)

and it's the creation of the fred object that's giving me the runtime
error. This must be a simple thing but I can't spot it anywhere.

Sun
 
A

Andrew Thompson

M

mekane

and a constructor something like this:
public support(int[] c, int[] d)
{
for (int i=0; i<x.length; i++)
{
x = c;
}
for (int i=0; i<y.length; i++)
{
y = d;
}
}

and support compiles fine.

Trouble is, when I come to pass the arguments to support from tester I
get a NullPointerException

E.g. in tester I have
int[] j = {1,2,3,4,5,6};
int[] k = {0,1,2,3,4,5};
support fred = new support(j,k)

and it's the creation of the fred object that's giving me the runtime
error. This must be a simple thing but I can't spot it anywhere.


Where does the NullPointException occur? Is it somewhere inside the
constructor or is it the constructor call itself?

I would try using the length of your input arrays as the limit in your
for loops. I'm not positive without trying it, but this would avoid the
possibility of trying to assign to an element within x that doesn't
exist. Since you didn't show how you initialize x and y this might be a
problem. So:

for (int i=0; i<c.length; i++) for the first one and
for (int i=0; i<y.length; i++) for the second one

might help.

-marty
 
S

sunstormrider

and a constructor something like this:
public support(int[] c, int[] d)
{
for (int i=0; i<x.length; i++)
{
x = c;
}
for (int i=0; i<y.length; i++)
{
y = d;
}
}

and support compiles fine.
Trouble is, when I come to pass the arguments to support from tester I
get a NullPointerException
E.g. in tester I have
int[] j = {1,2,3,4,5,6};
int[] k = {0,1,2,3,4,5};
support fred = new support(j,k)
and it's the creation of the fred object that's giving me the runtime
error. This must be a simple thing but I can't spot it anywhere.

Where does the NullPointException occur? Is it somewhere inside the
constructor or is it the constructor call itself?

I would try using the length of your input arrays as the limit in your
for loops. I'm not positive without trying it, but this would avoid the
possibility of trying to assign to an element within x that doesn't
exist. Since you didn't show how you initialize x and y this might be a
problem. So:

for (int i=0; i<c.length; i++) for the first one and
for (int i=0; i<y.length; i++) for the second one

might help.

-marty- Hide quoted text -

- Show quoted text -


Thanks - but I've found the error now - needed to create a new
instance of the variables within the constructor - then worked fine.
 
P

Patricia Shanahan

private int[] x
private int[] y

and a constructor something like this:

public support(int[] c, int[] d)
{
for (int i=0; i<x.length; i++)
{
x = c;
}
for (int i=0; i<y.length; i++)
{
y = d;
}
}


"private int[] x" leaves x null. If x is null, referencing x.length will
cause a null pointer exception.

You need to do something like "x = new int[c.length];" before treating x
as though it referenced an array, rather than being null.

Patricia
 
L

Lew

public support(int[] c, int[] d)
{
for (int i=0; i<x.length; i++)
{
x = c;
}
for (int i=0; i<y.length; i++)
{
y = d;
}
}

Thanks - but I've found the error now - needed to create a new
instance of the variables within the constructor - then worked fine.

Another approach that might help:
<http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#copyOf(int[], int)>

import java.util.Arrays;
public class Support
{
/* Consider following the source-code convention:
* name classes with an initial upper-case letter.
*/

private final int [] x;
private final int [] y;

/** Constructor.
* @param c <code>int []</code> source array.
* @param d <code>int []</code> source array.
* @throws NullPointerException
* if <code>c</code> or <code>d</code> is <code>null</code>.
*/
public Support( int [] c, int [] d )
{
x = Arrays.copyOf( c, c.length );
y = Arrays.copyOf( d, d.length );
}
// ... the rest of the class
}
 
P

Patricia Shanahan

mekane wrote:
....
I would try using the length of your input arrays as the limit in your
for loops. I'm not positive without trying it, but this would avoid the
possibility of trying to assign to an element within x that doesn't
exist.
....

That's always something to consider when things are going wrong, but it
has its own exception, ArrayIndexOutOfBoundsException, and so would not
cause a NullPointerException.

Patricia
 
L

Lew

(e-mail address removed) wrote,
Roedy said:

Now that is useful, direct, and helpful advice. To the OP: Take Roedy's
advice for what it is, something that will make you a better programmer. Some
might think that he should have couched his comment in sugar - "I hope you
don't mind, but in my response I'll recast the class name to an initial
upper-case letter in order to conform to widely-accepted Java coding
conventions, blah blah." The message would have been identical - here's what
should be, here's the link that explains it in detail. The conventions in
question, guidelines really, were published by Sun in 1999 and are mostly
followed by just about everybody. They should be mostly followed by everybody.
Always post your actual code. The devil is in the details.

Roedy is one of those with great wisdom in these matters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top