#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    BlueJ compiler error - cannot find symbol - variable <object>


    Hello all,

    Trying to write this piece of code for University, and I have been stuck for hours trying to resolve this painful error. Get the feeling It's quite simple to fix, but I have tried countless searches to find an answer -

    I have a Main object with the following code:

    Code:
    public class Main  {
        
        public static void main(String[] args) {
            UI userInterface = new UI();
            Game game = new Game();
        }
    }
    The UI class contains the following code:

    Code:
    import java.util.Scanner;
    
    public class UI{
        
        Scanner scanner = new Scanner(System.in);
        
        public UI() {
        
        }
       
        public int initialise(String descriptor, String variable) {
            System.out.println("Enter " + descriptor + " " + variable + " coordinate: ");
            return scanner.nextInt();
        }
        
    }
    And the Game class contains:

    Code:
    public class Game {
        
        public Game() {
            Player player = new Player();
        }
    
    }
    The Player class is where I am having problems. It contains the following...

    Code:
    public class Player  {
        
        private int positionX = 0;
        private int positionY = 0;
    
        public Player() {
            positionX = userInterface.initialise("your", "x");
            positionY = userInterface.initialise("your", "y");
        }
    }
    All my other classes compile fine, but this one returns the following compiler error: "cannot find symbol - variable userInterface" in reference to the line "positionX = userInterface.initialise("your", "x");

    I guess it's something to do with ownership? I can access "userInterface" from within Main.main() with no problems.

    I apologise if this is a really simple error, but I have put in a lot of effort to try and solve it.

    Thank you very much in advance, the web is really a wonderful place to have communities such as this =)
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,740
    Rep Power
    1959
    Where in the Player class have you specified what userInterface is?

    You should move
    Code:
    UI userInterface = new UI();
    into the Player class where you call the UI functions.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    103
    you have declared userInterface variable in your main class.
    Code:
    public class Main  {
        
        public static void main(String[] args) {
            UI userInterface = new UI();
            Game game = new Game();
        }
    }

    you are trying to use that variable in another class due to which you are getting the compiliation error.

    We can access the variables if they are declared locally in the method or if they are declared globally.

    If you want userInterface object in Player class, you need to create a new object inside your player class.
    Do this before you try to use userInterface object

    UI userInterface = new UI();
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0
    Surprise surprise, it works =)

    Thanks heaps, I can finally start being productive again!

    Just out of curiosity though, is there any way I can tell the Player class I am using an object initialised by main? 'Cause I will be having a few objects using the UI class, and I was hoping I'd only need one instance of it.

    I thought making the object public was supposed to allow me to access it from anywhere...

    Sorry for the questions, I'd just like to understand Java better.

    Thanks again for your reply, don't know what I would have done otherwise =)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    103
    Originally Posted by ben.w.barnes
    Surprise surprise, it works =)
    is there any way I can tell the Player class I am using an object initialised by main? 'Cause I will be having a few objects using the UI class, and I was hoping I'd only need one instance of it.
    you can always pass the object as a parameter while invoking the method.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0
    Cool, thanks. I will try that, I think it will streamline my program a bit doing it that way.

    Thanks everyone for your help.
  12. #7
  13. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,740
    Rep Power
    1959
    Originally Posted by ben.w.barnes
    Cool, thanks. I will try that, I think it will streamline my program a bit doing it that way.

    Thanks everyone for your help.
    From a logic point of view, it will be more sense to let the Game class control the UI instead of main.

IMN logo majestic logo threadwatch logo seochat tools logo