Jump to content
  • Advertisement
Sign in to follow this  
Moose6912

[java] Declaring an array of objects.

This topic is 5178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've declared an array of objects as such (MAX is declared as 5) Actor ActorList[] = new Actor[MAX]; for (iCount = 0;iCount < MAX;iCount++) { ActorList[iCount] = new Actor (this,41,37); } Then i've got an error when i've tried this line System.out.println(ActorList[3].iLength) The error is "Unresolved symbol ActorList".iLength is a public integer and i've already constructed the object in the for loop.So i can't understand why i still have this error.

Share this post


Link to post
Share on other sites
Advertisement
In Java, the declaration for arrays is:

Actor[] ActorList = new Actor[MAX];

The only other thing I can think of is that the variable is out of scope when you're trying to print it, or that the Actor class can't be found when you're compiling.

Share this post


Link to post
Share on other sites
Quote:
Original post by aWG
In Java, the declaration for arrays is:

Actor[] ActorList = new Actor[MAX];

No. The square brackets can be either on the type or on the variable. It is not fixed.

Quote:
Original post by aWG
The only other thing I can think of is that the variable is out of scope when you're trying to print it, or that the Actor class can't be found when you're compiling.


This is probably the case. You would have to post your full code to see what the problem is.

Share this post


Link to post
Share on other sites
Quote:
Original post by aWG
In Java, the declaration for arrays is:

Actor[] ActorList = new Actor[MAX];


Actually, you can declare arrays the old C-sytle way in Java (though most people don't consider it good practice), so the declaration shouldn't be the problem (but who knows, try it the other way and see). Other than that, it all looks fine. Are you sure you copied all relevent information to the post correctly?

EDIT: Heh, I guess CaptainJester and I were posting about the same time and I corrected something he already had. Sorry! :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Moose6912

Actor ActorList[] = new Actor[MAX];

Then i've got an error when i've tried this line
System.out.println(ActorList[3].iLength)

The error is "Unresolved symbol ActorList".iLength is a public integer and i've already constructed the object in the for loop.So i can't understand why i still have this error.


1. You are supposed to use LOWER CASE first letter for variable names (which is confusing because it can make readers think your error is a class that cannot be found)

2. It's saying it can't find anything with name "ActorList", i.e. the same as if you'd typed:

String anObject = "hello";
System.out.println( anBoject ); // typo in this line, generates "Unresolved symbol"

So, as suggested, the problem is probably that you're trying to access the ActorList var out of scope - e.g. it's only defined in one method, and you tried to call it from another method, where it doesn't physically exist!

redmilamber

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!