This site requires JavaScript, please enable it in your browser!
Greenfoot back
NullPointer
NullPointer wrote ...

2014/1/4

cannot find symbol - method

NullPointer NullPointer

2014/1/4

#
Hi, I wanted to program a board game with greenfoot and already finished one project. But it was coded very poorly and now I wanted to try the same game again in a better way. In the game, there are 16 figures and I wanted to save them in an array:
public class Figur extends Spiel
{
    private int figurID;
    private String color;
    private Figur[] figuren = new Figur[16];

    public void act(){

        //Add the figures to the array
        for(int i = 0; i < 4; i++){
            figuren[i] = new Figur( i, "Gruen");         
        }
        for(int j = 4; j < 8; j++){
            figuren[j] = new Figur(j, "Rot");
        }
        for(int k = 8; k < 12; k++){
            figuren[k] = new Figur(k, "Blau");
        }
        for(int l = 12; l < 16; l++){
            figuren[l] = new Figur(l, "Gelb");
        }   
    }

    /**
     * Constructor of Figur
     * @param int yfigurID, String ycolor  
     */
    public Figur(int yfigurID, String ycolor){
        this.figurID = yfigurID;
        this.color = ycolor;
    }
To give out the object of the array, I wrote this method (also in the class "Figur"):
    public Figur getFigur(int p){
        return figuren[p];
    }
I want to use this method in the over class "Spiel" but it says that it cant find the method. (I want to use it like this: "getFigur(5).command;") Any ideas how this could happen? Or maybe a better suggestion for the management of the figures?
danpost danpost

2014/1/4

#
If it is saying that it cannot find the 'getFigur' method from a class other than 'Spiel', then you are probably not letting the system know to look in the 'Spiel' class for that method. From an Actor subclass (with an actor that is in the world), you would use the 'getWorld' method and typecast the returned world to a Spiel world object:
Spiel spiel = (Spiel)getWorld();
spiel.getFigur(5).command();
Without the typecasting, the system does not know to look in any subclass of the World class (which is the type of object that 'getWorld' returns).
You need to login to post a reply.