Results 1 to 5 of 5
  1. #1
    JonoHart is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default Semantic error: Cannot reach instance method: from Static Context

    Semantic error: Cannot reach instance method: moveAndCheckForWin( RandomFrog ) from static context: Game

    I've writen an orchestrator class to send messages to instances of another class which compiles fine but when call the message "moveAndCheckForWin();" i get the above error.

    I'm using a IDE called Blue J
    Java Code:
    import ou.*;
    /**
     * Class Game - write a description of the class here.
     * 
     * @author () 
     * @version (24/01/2011_01)
     */
    public class Game
    {
    	/* instance variables */
    	private RandomFrog randomFrog1; 
    	private RandomFrog randomFrog2;
    	private RandomFrog randomFrog3;
    	private RandomFrog winner;
    	private boolean gameOver;
    
    
    	/**
    	 * Constructor for class Game taking 3 instances of frog, changing the
    	 * colour of each frog so they all have different colours and setting the 
    	 * instance of gameOver to false
    	 */
    	public Game(RandomFrog rf1, RandomFrog rf2, RandomFrog rf3)
    	{
    		this.randomFrog1 = rf1;
    		this.randomFrog2 = rf2;
    		this.randomFrog3 = rf3;
    		randomFrog1.setColour(OUColour.BLUE);
    		randomFrog2.setColour(OUColour.RED);
    		randomFrog3.setColour(OUColour.YELLOW);
    		this.gameOver = false;
    	}
    
    
    	/* instance methods */
    
    	/**
    	 * An example of a method - replace this comment 
    	 * and the method below with your own
    	 */
    	public void moveAndCheckForWin(RandomFrog aFrog)
    	{
    		
    	   aFrog.setPosition(aFrog.nextPosition());
    		
    	}
    }
    setPosition and nextPosition are methods in RandomFrog and both are set to Public.
    Java Code:
     /**
        * Method to generate at random a number between 1 and 11
        */
       public int nextPosition()
       {
          return rand.nextInt(11)+1;
       }
    Java Code:
     /**
        * Method to override setPosition in Frog
        * if the argument received is between 1 and 11 inclusive then
        * set as ordinary Frog object. otherwise generate a random position 
        * between 1 and 11 and set position accordingly
        */
       public void setPosition(int aNumber)
       {
          if((aNumber>0) && (aNumber<12))
          {
             super.setPosition(aNumber);
          }
       else
          {
             super.setPosition(nextPosition());
          }
       }
    Can anyone explain why it is not being allowed access?

    Many Thanks

  2. #2
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Semantic error: Cannot reach instance method: moveAndCheckForWin( RandomFrog ) from static context: Game
    It says you are calling it from a static context. I don't see where you are calling it from.

  3. #3
    JonoHart is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    I've sent these messages in my workspace

    Java Code:
    RandomFrog rf1 = new RandomFrog();
    RandomFrog rf2 = new RandomFrog();
    RandomFrog rf3 = new RandomFrog();
    Game game1 = new Game(rf1, rf2, rf3);
    Game.moveAndCheckForWin(rf1);

  4. #4
    JonoHart is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    just seen it

    game.moveAndCheckForWin(rf1);

    should read

    game1.moveAndCheckForWin(rf1);

    Sorry for messing you about

    Proper Stupid mistake

    Thanks for trying to help!!!

    Jon

  5. #5
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 12:12 PM
  2. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 05:05 AM
  3. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 09:51 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •