• Post Reply Bookmark Topic Watch Topic
  • New Topic

objects referencing other objects - cannot find symbol error  RSS feed

 
Jose Castillo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there folks,

Basically, I am writing a (very) basic text based game for class. In my main method, I instantiate all of my objects. They include rooms, items, creatures, and exits. In order to create an exit, I need to provide a room that the exit leads to. Basically



But for a room, I need to provide the exit. Like this constructor, that accounts for a single exit and a creature:



So, in the main method, if I do this (Excuse the lame creatures, and whatnot. I'm definitely not a writer ):



I get the Exit objects saying they can't find symbol, say, roomFour. If I do it where the rooms are above the exits, I get i.e., can't find variable, roomEighttoTen. I am quite sure I am just making some dumb mistake, but could I get some helping pointers? I'm pretty new to this still I tried instantiating the exits and rooms with null values, but I got null pointer exceptions.


Note: my specific error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous ctor sym type: <any>
at TextAdventure.TextAdventure.main(TextAdventure.java:10)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you have cyclic dependencies.. your room depends upon exit and ext depends upons the room. It is not good. Please think about your design again.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could fix the cyclic-dependency problem by having your Room object have a setExit() method, which you call after declaring all the Rooms and then all the Exits. However your problem at the moment is that the "new Room(...)" lines in your code don't correspond to how you declared your Room constructor. Your constructor wants a Creature object third and an Exit object fourth, whereas your code is putting the Creature last (which is wrong) and puts in more than one Exit (which is also wrong).

So you need to change the code which creates Room objects. You may also need to change the Room object so that it can contain more than one Exit object -- from your variable names it looks like you do need that.
 
Jose Castillo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've figured it out, I think, or at least made it run. What I've done is removed the need for exits in the methods for Rooms. Then, later in the main method, I've used a mutator method to set the exits. Here it is, for future people maybe seing this:




Obviously a few improvements can be made, but for something that's due in the morning I'd say it's enough ;) Thank you to all who helped and sparked my mind. You're all awesome for helping newbies, and when I am an expert at Java, I will definitely repay the favor around here.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good fix, so congratulations.

But... setExit1, setExit2, setExit3? That's a bit icky. It would be better for the Room to have a list of Exit objects, rather than exactly three. Then you'd just need an addExit() method to add an Exit to a Room. That's also going to make it simpler to deal with the Exits of a Room when you have to, because you just go through the list rather than writing code for each of the exit1, exit2, and exit3 variables.

(There's a design principle called "Zero, One, Infinity"... if a type can contain references to another type, then it should have either zero references, one reference, or "infinity" references. The "infinity" here means a list of references which can be of any size, not a predetermined size. Clearly "Three" doesn't follow this principle.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!