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

2013/5/8

getY(), Nullpointerexception error

1
2
erdelf erdelf

2013/5/8

#
it seems that if I call the getY() method in the act method in a specific area I get a Nullpointerexception.
if(getY() > getWorld().getHeight()-100 && 
        getY() < getWorld().getHeight()-50)
Does anyone know how to fix that? btw. I can see the Object clearly in the world
bourne bourne

2013/5/8

#
Need more context.
Gevater_Tod4711 Gevater_Tod4711

2013/5/8

#
Are you shure that it is the getY method that throws the NullPointerException? Because there is no point or no object which you want to reference with a point. getY() is the same than this.getY() but if this == null then the method (act or whatever) could also never be called. So I think this error probably occours somewhere else.
bourne bourne

2013/5/8

#
I was thinking getWorld() returned null if indeed the Exception occurred in the provided code.
danpost danpost

2013/5/8

#
bourne wrote...
I was thinking getWorld() returned null if indeed the Exception occurred in the provided code.
Correct. As 'getY()' would create an IllegalStateException if the actor was not in the world. If the actor was removed from the world during this act cycle but prior to this statement, 'getWorld' would throw a NullPointerException.
Gevater_Tod4711 Gevater_Tod4711

2013/5/8

#
erdelf wrote...
btw. I can see the Object clearly in the world
But if the object is removed you couldn't see it in the world.
erdelf erdelf

2013/5/8

#
I am calling this every act cycle, but the error only appears in this small area getY() > getWorld().getHeight()-100 && getY() < getWorld().getHeight()-50 I am sure that the getY method is throwing the error because I splitted the lines to test this, so that every word has its own line, and the error was still in the line getY()
erdelf erdelf

2013/5/9

#
anyone an idea?
Gevater_Tod4711 Gevater_Tod4711

2013/5/9

#
I don't even know how a getY method could throw a NullPointerException. Could you post the stack trace? Maybe that'll help.
erdelf erdelf

2013/5/9

#
line 35 in Block is the line with getY()
java.lang.NullPointerException
	at Block.checkKey(Block.java:35)
	at GameWorld.act(GameWorld.java:44)
	at greenfoot.core.Simulation.actWorld(Simulation.java:571)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
Gevater_Tod4711 Gevater_Tod4711

2013/5/9

#
Block.java:35 is not the line of the error I think. Probably the nullPointerException is thrown of something else in this method. If you click on the link of the stack trace it'll show you where the error occours. Or you could post the code of the method checkKey here. Maybe we can help you then.
erdelf erdelf

2013/5/9

#
    public void checkKey(String string)
    {
        if(getY() > getWorld().getHeight()-100 && 
        getY() < getWorld().getHeight()-50 && 
        string.equals(getChar()))
            if(getState()==0) 
                changeState(1);
    }

Gevater_Tod4711 Gevater_Tod4711

2013/5/9

#
Well the only things that could throw NullPointerExceptions in this method are getWorld(). ... and string.equals(); So either the object has been removed from the world before executing this code or you call the method with a parameter null (checkKey(null)). To know what happens there I'd need to see the whole code.
erdelf erdelf

2013/5/9

#
World act
public void act()
{
        List blocks = getObjects(Block.class);
        String str = Greenfoot.getKey();
        Block block;
        for(int i = 0; i<blocks.size(); i++)
        {
            block = (Block)blocks.get(i);
            block.checkKey(str);
        }
}
Block
    public void checkKey(String string)
    {
        if(getY() > getWorld().getHeight()-100 && 
        getY() < getWorld().getHeight()-50 && 
        string.equals(getChar()))
            if(getState()==0) 
                changeState(1);
    }
    public int getState()
    {
        return state;
    }

    public String getChar()
    {
        return str;
    }
    public void changeState(int status)
    {
        GreenfootImage img = new GreenfootImage(50,50);
        this.state = status;
        img.setColor(Color.BLUE);
        img.drawOval(0,0,50,50);
        img.drawOval(1,1,46,48);
        img.drawOval(2,2,46,46);
        img.drawOval(3,3,45,45);
        if(status == 0) 
            img.setColor(Color.WHITE);
        else if(status == 1)
            img.setColor(Color.GREEN);
        else if(status == 2)
            img.setColor(Color.RED);
        img.fillOval(3,3,44,44);
        img.setColor(Color.RED);
        img.setFont(new Font("Arial", Font.BOLD, 30));
        img.drawString(str, 13, 35);
        setImage(img);
    }
Gevater_Tod4711 Gevater_Tod4711

2013/5/9

#
The problem is in your world class in line 4 and 9. You always call the method checkKey with the parameter str which has the value of getKey(). But if there is no key pressed the getKey method returns null. So you call the checkKey method with the parameter null which causes the error in line 5 of your check key method in the class Block. You should first check whether the string is not null before calling the method:
    public void act()  
    {  
            List blocks = getObjects(Block.class);  
            String str = Greenfoot.getKey();  
            Block block;  
            if (str != null) {
                for(int i = 0; i<blocks.size(); i++)  
                {  
                    block = (Block)blocks.get(i);  
                    block.checkKey(str);  
                }
            }
    }  
Then it'll work.
There are more replies on the next page.
1
2