Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Strange Pointer Problems
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HyperFlexed




PostPosted: Thu Nov 10, 2005 12:02 pm   Post subject: Strange Pointer Problems

I am trying to create an array of objects like so:

code:

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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Nov 10, 2005 2:09 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 10, 2005 7:39 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 10, 2005 7:41 pm   Post subject: (No subject)

I'll need to see the rest of your code. I suspect you're getting caught by the reference nature of objects.
HyperFlexed




PostPosted: Thu Nov 10, 2005 7:49 pm   Post subject: (No subject)

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.



finalLogo.zip
 Description:
final.logo zipped.

Download
 Filename:  finalLogo.zip
 Filesize:  242 Bytes
 Downloaded:  83 Time(s)


LogoCmd.java
 Description:

Download
 Filename:  LogoCmd.java
 Filesize:  2.24 KB
 Downloaded:  109 Time(s)


Logo.java
 Description:

Download
 Filename:  Logo.java
 Filesize:  18.95 KB
 Downloaded:  130 Time(s)

wtd




PostPosted: Thu Nov 10, 2005 8:17 pm   Post subject: (No subject)

I see this:

Java:
LogoCmd[] commands = new LogoCmd [commandsInFile];


And then in a loop a few lines later I see:

Java:
commands [cmdCounter].setValues ((String) tokens.get (tok), Integer.parseInt ((String) tokens.get (tok + 1)));


But in between there is no initialization of the contents of the "commands" array.
HyperFlexed




PostPosted: Thu Nov 10, 2005 8:19 pm   Post subject: (No subject)

I tried doing this, to no avail... Is this proper?

code:

        //Fill array with buffer objects
        for (int object = 1; object <= commandsInFile; object++)
        {
            commands [object-1] = new LogoCmd ("fd",-1);
        }
wtd




PostPosted: Thu Nov 10, 2005 8:37 pm   Post subject: (No subject)

Possibly. Honestly, your code is very awkward to read, and is hard to debug, especially since I don't have the HSA classes, and therefore cannot compile the code.

Perhaps if you showed me the exact error messages and the exact code you're using at the time, I could help.
Sponsor
Sponsor
Sponsor
sponsor
HyperFlexed




PostPosted: Thu Nov 10, 2005 10:45 pm   Post subject: (No subject)

But I'm not getting error messages Crying or Very sad

I'm just going to fudge this one tommorow. I'll make 2 arrays, one for the string commands, and one for the integer magnitudes, pass with a 70 and conquer this stupidness another day when my mark doesn't depend on it.

be it resolved that deadlines suck. Thanks for the help though. Bah, I still have world history homework.

Oh, and any one thing that makes my code akward to read? Maybe I should improve my skills.


Also, if one were making an array of objects, what would be the right way so that all the elements aren't null? You said my for loop would "possibly" work, but how would you do it? (seeing as you seem very knowledgeable about programming)
HyperFlexed




PostPosted: Fri Nov 11, 2005 11:46 am   Post subject: (No subject)

My teacher and I solved the problem. If you look in the source to the LogoCmd class, you'll notice some of the variables were declared as static. From what I half gather they would not have been constructed when the class was instantiated? The vars were static because some of my methods were also static which I learned you don't do in a class....

JAVA IS MESSY! wow. It makes no sense.

I'm not fully sure what static does anymore, but I have learned that I don't like it Razz

Can anyone enlighten me as to static's true purpose?
wtd




PostPosted: Fri Nov 11, 2005 2:03 pm   Post subject: (No subject)

My tutorial has a good section on static context.
HyperFlexed




PostPosted: Sun Nov 13, 2005 4:34 pm   Post subject: (No subject)

So if a method is static in a class when the class is instantiated, does that mean that the method won't be useable in the sense of an object? I gathered that much on a site, it said that static methods apply to all objects of a given type rather than the objects of that type.

I see now what static does, but I can't think of any uses for it.
wtd




PostPosted: Sun Nov 13, 2005 4:56 pm   Post subject: (No subject)

You have to think in terms of context.

There is a class context, and there is an object context. Objects carry with them their own object context, but also the context of the class they belong to.

Thus static methods can be used in either the class or object context. However, static methods do not have an object context, and thus we cannot implicitly use object methods within a static method.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: