DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2004

    initiating an array twice

    I'm having trouble understanding something:

    Say I create a array

    private One[] array1;

    and initialise it in the constructor method of my class

    array1 = new One [10];

    and set the values for each element in the array in the body of a loop,

    for(int count = 0; count <10; count ++)
    array1 = new One();

    where setAlive is a method in class One which changes the values of the private variables of class One.

    Now, late in the program a method that gets called , in this message I again say

    public void setAliveTwo( int x)
    array1 = new One();

    Where setDead is also a method in class One that changes the values of class One's private variables.
    Because I want to change the value of a specific object in the array.

    But this does not work.

    If I dont include array1 = new One(); I get a null point exception. If I do, it seems to create a new array element instead of changing the original one.

    Is this what's happening? Can there be more than one array, called array1? Two objects in array1 with the same index number? I'm confused.

  2. #2
    Join Date
    Feb 2004
    change what you have to this. For the loop that fills the array change it to
    for(int count = 0; count <10; count ++)
        array1[count] = new One();
    in the methods you are making a whole new One object. If you can't get it to work without that line it, then just add the array to the parameters of the method like
    public void setAliveTwo( One[] array1, int x)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.