File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes object array compile error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object array compile error" Watch "object array compile error" New topic

object array compile error

Bob Joness

Joined: May 20, 2004
Posts: 4
i'm having compilation problems with this piece of code. The problem is with the 'x' in the lines arrayOfCars[x] = tempBuyCar; and System.out.println(arrayOfCars[x]);

i get the error 'cannot resolve symbol'. however if i replace the x with a number it compiles.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

This one is easy. since x is declared inside the "for" statment, it falls out of scope once you leave the loop. you don't have braces after your "for" statement so, the only line that is part of your loop is the "tempBuyCar" line. that is the ONLY line being run over and over.


when you get to the arrayOfCars, x has fallen out of scope, thus the compiler can't resolve it.

do this:

and life should be good.
[ May 20, 2004: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bob Joness

Joined: May 20, 2004
Posts: 4
yep that fixed it. can't believe how simple it was.

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Some beginners, and some folks with a bit of experience, like myself, always include opening and closing braces, with constructs such as for-loops and if-statements.

It's also a required/recommended practice by some coding syle guides, such as JavaRanch's style guide. The reasoning for this is that consistency is easier to read, less editing is involved if lines of code are added or removed, plus it becomes very obvious what code is associated with the construct.

Also, I strongly recommend that you make a practice of properly indenting your code, as this will strongly help in its readability.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: object array compile error
It's not a secret anymore!