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

2013/1/10

non-static method-non-static-non-static-non-static...

Minion1 Minion1

2013/1/10

#
AAAAAAAAAAAAAAAAAARRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGHHHHHHHHH~! I get this non-static method thing all the time, and I hate to admit that despite my many attempts at understanding it I just don't get it. It's getting very frustrating. Most other errors I can start to pick apart, but this one is like a brick wall. Here's the problem: there are two classes to deal with here, Scroller and it's subclass Cursor. Scroller handles the actual motion, Cursor is supposed to provide a color detection value. Scroller is trying to call the redCheck() method from Cursor. Here's the relevant code for Scroller:
    public void checkKeyPress()
    {
        timer++;
        if(Greenfoot.isKeyDown("W") && timer >= 10)        
        {
            setLocation(getX(), getY() + MOVEDISTANCE);
            Cursor.redCheck();
            if(whereAmI == "end")
            {
                setLocation(getX(), getY() - MOVEDISTANCE);
            }
            if(whereAmI == "forest" && forester == false)
            {
                setLocation(getX(), getY() - MOVEDISTANCE);
            }
            else if(whereAmI == "mountain" && mountaineer == false)
            {
                setLocation(getX(), getY() - MOVEDISTANCE);
            }
            else if(whereAmI == "water" && boat == false)
            {
                setLocation(getX(), getY() - MOVEDISTANCE);
            }
            //randomEncounter = randomEncounter + 1;
            timer = 0;
        }
        else if(Greenfoot.isKeyDown("A") && timer >= 10)
        {
            setLocation(getX() + MOVEDISTANCE, getY());
            //redCheck();
            if(whereAmI == "end")
            {
                setLocation(getX() - MOVEDISTANCE, getY());
            }
            if(whereAmI == "forest" && forester == false)
            {
                setLocation(getX() - MOVEDISTANCE, getY());
            }
            else if(whereAmI == "mountain" && mountaineer == false)
            {
                setLocation(getX() - MOVEDISTANCE, getY());
            }
            else if(whereAmI == "water" && boat == false)
            {
                setLocation(getX() - MOVEDISTANCE, getY());
            }
            //randomEncounter = randomEncounter + 1;
            timer = 0;
        }
        else if(Greenfoot.isKeyDown("S") && timer >= 10)
        {     
            setLocation(getX(), getY() - MOVEDISTANCE);
            //redCheck();
            if(whereAmI == "end")
            {
                setLocation(getX(), getY() + MOVEDISTANCE);
            }
            if(whereAmI == "forest" && forester == false)
            {
                setLocation(getX(), getY() + MOVEDISTANCE);
            }
            else if(whereAmI == "mountain" && mountaineer == false)
            {
                setLocation(getX(), getY() + MOVEDISTANCE);
            }
            else if(whereAmI == "water" && boat == false)
            {
                setLocation(getX(), getY() + MOVEDISTANCE);
            }
            //randomEncounter = randomEncounter + 1;
            timer = 0;
        }
        else if(Greenfoot.isKeyDown("D") && timer >= 10)
        {       
            setLocation(getX() - MOVEDISTANCE, getY());
            //redCheck();
            if(whereAmI == "end")
            {
                setLocation(getX() + MOVEDISTANCE, getY());
            }
            if(whereAmI == "forest" && forester == false)
            {
                setLocation(getX() + MOVEDISTANCE, getY());
            }
            else if(whereAmI == "mountain" && mountaineer == false)
            {
                setLocation(getX() + MOVEDISTANCE, getY());
            }
            else if(whereAmI == "water" && boat == false)
            {
                setLocation(getX() + MOVEDISTANCE, getY());
            }
            //randomEncounter = randomEncounter + 1;
            timer = 0;
        }
        if(timer > 20)
        {
            timer = 10;
        }
    }
And here is the relevant code for Cursor:
public void redCheck()
    {
        int red = getWorld().getColorAt(getX(), getY() - 5).getRed();
        //System.out.println(red);

        if(red == 95)
        {
            whereAmI = "forest";
        }
        else if(red == 195)
        {
            whereAmI = "cave";
        }
        else if(red == 171)
        {
            whereAmI = "castle";
        }
        else if(red == 0)
        {
            whereAmI = "water";
        }
        else if(red == 198)
        {
            whereAmI = "town";
        }
        else if(red == 220)
        {
            whereAmI = "path";
        }
        else if(red == 166)
        {
            whereAmI = "mountain";
        }
        else if(red == 145)
        {
            whereAmI = "end";
        }
    }
All I want is for the scroller to call the redCheck() method from Cursor succesfully. It needs the red value to use the movement code.
Gevater_Tod4711 Gevater_Tod4711

2013/1/10

#
probably you are trying to call a method of your cursor class (l.7) which is not static. If you want to call the method you have to use a reference to the cursor object.
danpost danpost

2013/1/10

#
You probably are trying to call the method on the class instead of on an object of the class. That is,
//trying
Cursor.redCheck();
// instead of
((Cursor)getWorld().getObjects(Cursor.class).get(0)).redCheck();
You may have to check to see if a cursor is in the world first (or a nullPointerException will occur). Just remember that most things are done to objects and not to the class. For example, you cannot use 'setLocation' on a class, it has to be called on an object; you cannot 'add' a value to a Counter class, the counter that is created from the class contains the value that needs changed. Maybe understanding what 'static' fields and methods are may help. A 'static' field is something that is shared amoung all objects of the class. A 'static' field is also known as a class field (because an instance of that field is not given to each instance of the class). The Java trail, Learning the Java Language, has a clearer explanation of this. 'Static' methods are those that are not object pecific; usually called to either do something with all the objects of that class or do something (like change the value of a 'static' field) that relates to all the objects of that class.
Minion1 Minion1

2013/1/10

#
Thank you danpost. I always write down your responses as "notes," since you have a way of explaining these things. I will try that.
danpost danpost

2013/1/10

#
BTW, just to let you know, unless specified as 'static', all fields and methods are object instance type.
Minion1 Minion1

2013/1/10

#
Ok, that got me past the static problem. So now it's calling the method of the Cursor object, which is in the world already. Now I have a new problem, it can't seem to use the redCheck method. It keeps getting stuck with a null pointer exception here:
public void redCheck()

    {
        int red = getWorld().getColorAt(getX(), getY() - 5).getRed(); -----------THIS LINE
        System.out.println(red);
Minion1 Minion1

2013/1/10

#
I should mention that the Scroller class is moving a background map around, but the Cursor is a subclass of Scroller that does NOT move around but remains stationary. What I'm trying to do specifically is detect the level of red from a specific pixel underneath the stationary Cursor. I've been struggling with scrolling worlds for days now, I've downloaded several examples, and ultimately realized the simplicity of moving the background instead of the foreground is the simplest way to go.
danpost danpost

2013/1/10

#
You are probably calling the 'redCheck' method either before the Cursor object is placed in the world or after it has been removed. You could put a check as the first statement in the 'redCheck' method to avoid the error:
public void redCheck()
{
    if (getWorld() == null) return;
    // the method as you have it
}
You need to login to post a reply.