
-----------------------------------
HyperFlexed
Thu Nov 10, 2005 12:02 pm

Strange Pointer Problems
-----------------------------------
I am trying to create an array of objects like so:


MyObject arrayOfObjects = new MyObject[arbitraryNumberOfElements];


however, when I set any element to have a certain value using a method in my object, all of the objects in the array have their values changed to those values.

My hypothesis is that every element in the array is pointing to a single object, anyone know why this is happening? The due date for my project is impending... all my bits and worship to whoever can help me.

-----------------------------------
wtd
Thu Nov 10, 2005 2:09 pm


-----------------------------------
When you create a new array of objects in Java, you've simply created space for those objects.  You have not initialized any of those objects.  They are all "null".

-----------------------------------
HyperFlexed
Thu Nov 10, 2005 7:39 pm


-----------------------------------
Then:

1) how is at least one object existing?
2) how can I make these pointers point to objects/how do I create the objects?

When I assign an array element a value should I go

myArray[element] = new MyObject (params); ???

When I did this, I was having the issue still where modifying one item would make the values the same at every array element.

-----------------------------------
wtd
Thu Nov 10, 2005 7:41 pm


-----------------------------------
I'll need to see the rest of your code.  I suspect you're getting caught by the reference nature of objects.

-----------------------------------
HyperFlexed
Thu Nov 10, 2005 7:49 pm


-----------------------------------
In logo.java, there is a method called runLogoFile or something similar, it's the one giving me trouble. Excuse my use of the Console class, all part of the course but I do plan on learning the real way.

I have attached the LogoCmd class as well.

edit : attached a test data file, final.logo.

-----------------------------------
wtd
Thu Nov 10, 2005 8:17 pm


-----------------------------------
I see this:

LogoCmd

And then in a loop a few lines later I see:

commands 

But in between there is no initialization of the contents of the "commands" array.

-----------------------------------
HyperFlexed
Thu Nov 10, 2005 8:19 pm


-----------------------------------
I tried doing this, to no avail... Is this proper?


        //Fill array with buffer objects
        for (int object = 1; object 