Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

An easy one but where am I going wrong

Thread view: 
sunstormrider@hotmail.com - 21 Sep 2007 17:47 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.