Sign in to follow this  

[java] Declaring an array of objects.

This topic is 4865 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
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

This topic is 4865 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this