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

2014/1/28

Nonstatic variable cannot be referenced from a static context HELP!!!

cellostar47 cellostar47

2014/1/28

#
I am getting this error, yet NOTHING in my code is static!!! HELLPPP! Here is the code from the two classes that are interacting:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Piece here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Piece extends Actor
{
    public String player;
    public int color;
    public boolean isp1turn2;
    public int dumX;
    public int dumY;
    public Piece(int color){
        if(color == 1){
            setImage("blue-draught.png");
            player = "Blue";
            color = color;
            
        }
        if(color == 2)
        {
            player = "Red";
            color = color;
            
        }
        isp1turn2 = true;        
    }
    /**
     * Act - do whatever the Piece wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        try{
            makeKing();
        }
        catch(NullPointerException e){
        }
        takeTurn();
    }    
    public void makeKing()
    {
        if(player.equals("Red") && getY()<=0){
            getWorld().addObject(new King(color), getX(), getY());
            getWorld().removeObject(this);
        }
    }
    public void takeTurn()
    {
        if(isp1turn2 == true){      // is p1's turn:
            if(Greenfoot.mouseClicked(this)){
                getWorld().addObject(new Dummy(), getX()-1, getY()-1);
                getWorld().addObject(new Dummy(), getX()+1, getY()-1);
                setLocation(dumX, dumY);
            }
        }
    }
}
And the other.....
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Dummy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Dummy extends Actor
{
    public int xX;
    public int yY;
    public boolean isClicked;    
    /**
     * Act - do whatever the Dummy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        xX = getX();
        yY = getY();
        if(Greenfoot.mouseClicked(this)){            
            isClicked = true;
        }
        if(isClicked == true){
            Piece.dumX = xX;   //<== ERROR COMES HERE
            Piece.dumY = yY();  //<== ERROR COMES HERE
            isClicked = false;
        }
    }    
}
PLEASE HELP MEE!!!!
davmac davmac

2014/1/28

#
I am getting this error, yet NOTHING in my code is static!!! HELLPPP!
That is actually what the error message is saying. You are using the dumX and dumY variables as if they were static, but they are not. The problem is that in a line like this:
Piece.dumX = xX;
... you're not specifying which object you want to set the value of dumX for. You're just specifying the class ("Piece"). You need to get a reference to the object and set the value on it. You might want to check out tutorial #6.
danpost danpost

2014/1/28

#
Nothing may be static, but lines 26 and 27 are written in a form that would require static fields of 'dumX' and 'dumY'. These fields are given to each Piece object and can only be accessed through the objects themselves (not through the class, as the class only assigns them to each object created from the class; but the class does not have those fields to use itself). If I knew what you were trying to accomplish here, I might be able to help. But, too little information has been given at this point.
jayprich jayprich

2014/1/28

#
The "Piece.dumX = xX;" means you expect the "Piece" class itself to have a property called "dumX" shared between ALL instances .. this is possible for a static field but is probably not what you mean. Instead get a reference to a particular instance of a Piece - within "Act" for instance a Dummy may be touching a Piece or an existing one could be detected given the X and Y coordinates of a click. But it appears to me you actually want to create a new one. By the way "yY()" doesn't make sense as yY is a field and not a method. Something like the following would require you to add a single instance of the "Dummy" object to a world and once you are "running" (.. not stepping with a single "Act" or in design-mode) any clicks will try and add a new object of type Leaf at wherever you click
    public void act() 
    {
        MouseInfo gminf = Greenfoot.getMouseInfo(); // a static method of class Greenfoot
        if(gminf != null && gminf.getButton() == 1 && gminf.getActor() == null) // left button clicked and not on an existing object
        {
            Leaf newpiecetoplaceatmouseclick = new Leaf();
            getWorld().addObject(newpiecetoplaceatmouseclick, gminf.getX(),gminf.getY());
        }
    }
cellostar47 cellostar47

2014/2/4

#
I'll post the scenario here: http://www.greenfoot.org/scenarios/10772
danpost danpost

2014/2/4

#
See comment in scenario.
cellostar47 cellostar47

2014/2/5

#
Thanks!
You need to login to post a reply.