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[i] = c[i];
}
for (int i=0; i<y.length; i++)
{
y[i] = d[i];
}
}
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
Andrew Thompson - 21 Sep 2007 17:58 GMT
...
>This is driving me nuts - I thought I had it working and then not.
>
>I have 2 classes - "tester" and "support"
...
I am unable to spot the error in your snippets, but then,
I generally will not spend time looking at code snippets.
Usually, an SSCCE will describe the problem much
more clearly.
<http://www.physci.org/codes/sscce.html>

Signature
Andrew Thompson
http://www.athompson.info/andrew/
mekane - 21 Sep 2007 18:17 GMT
> and a constructor something like this:
>
[quoted text clipped - 22 lines]
> 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
sunstormrider@hotmail.com - 21 Sep 2007 18:21 GMT
> > and a constructor something like this:
>
[quoted text clipped - 40 lines]
>
> - 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.
Lew - 21 Sep 2007 19:30 GMT
>>> public support(int[] c, int[] d)
>>> {
[quoted text clipped - 7 lines]
>>> }
>>> }
> 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[],%20int)>
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
}

Signature
Lew
Patricia Shanahan - 21 Sep 2007 19:36 GMT
...
> 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
Patricia Shanahan - 21 Sep 2007 19:26 GMT
...
> private int[] x
> private int[] y
[quoted text clipped - 12 lines]
> }
> }
"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
Roedy Green - 22 Sep 2007 00:47 GMT
>I have 2 classes - "tester" and "support"
Classes begin with a capital letter. See
http://mindprod.com/jgloss/codingconventions.html
Always post your actual code. The devil is in the details.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 22 Sep 2007 19:05 GMT
sunstormrider@hotmail.com wrote,
>> I have 2 classes - "tester" and "support"
> Classes begin with a capital letter. See
> http://mindprod.com/jgloss/codingconventions.html
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.

Signature
Lew