initiating an array twice


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: initiating an array twice

  1. #1
    Join Date
    Apr 2004
    Posts
    3

    initiating an array twice

    Hi
    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();
    array1[count].setAlive();
    }

    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();
    array1[x].setDead();
    }

    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
    Posts
    541
    change what you have to this. For the loop that fills the array change it to
    Code:
    for(int count = 0; count <10; count ++)
    {
        array1[count] = new One();
        array1[count].setAlive(); 
    }
    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
    Code:
    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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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