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/9

#
thx for your help
davmac davmac

2013/5/9

#
Erdelf, I don't understand is why you said that this code:
    if(getY() > getWorld().getHeight()-100 &&   
            getY() < getWorld().getHeight()-50)
was causing a NullPointerException, when now it seems that's not the case?
danpost danpost

2013/5/9

#
@davmac, he apparently did not show the whole line initially; which was
if(getY() > getWorld().getHeight()-100 && 
   getY() < getWorld().getHeight()-50 && 
   string.equals(getChar()))
where 'string' had a value of 'null'.
davmac davmac

2013/5/9

#
Exactly - what I'm asking is, why post a line that wasn't actually causing the problem? I'm sure erdelf wasn't deliberately misleading us, but it seems silly to remove part of the line before posting it - especially when removing that part would stops problem from happening. I suppose what I'm really trying to say is: if you have a problem, please don't make assumptions about what is or what is not causing it without testing them, and definitely don't post code that isn't actually the code you're having the problem with.
Gevater_Tod4711 Gevater_Tod4711

2013/5/9

#
erdelf wrote...
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)
I think erdelf thought that the error was in line 35 because of the first line of the stack trace (java:35)
erdelf erdelf

2013/5/9

#
thx, that for the help. I thought the error would be the getY() call. btw. is it normal that if I hold the key down that the method getKey() always returns the key?
danpost danpost

2013/5/9

#
erdelf wrote...
btw. is it normal that if I hold the key down that the method getKey() always returns the key?
No. That would be normal for the 'isKeyDown' method. The 'getKey' method will return the value of any key that has been released. Once a key is returned using 'getKey', any future calls to 'getKey' will return 'null' until another key is released. This applies not only in general, but even within the same act cycle.
erdelf erdelf

2013/5/9

#
well you see my code on the last discussion page and if I hold the key down it always returns the same value
davmac davmac

2013/5/9

#
No. That would be normal for the 'isKeyDown' method. The 'getKey' method will return the value of any key that has been released. Once a key is returned using 'getKey', any future calls to 'getKey' will return 'null' until another key is released.
Actually, the getKey() returns any key that the system considers as having been typed. Exactly what constitutes typing a key is dependent on the system, but most have some sort of keyboard repeat - which means if you hold a key down, the key is 'typed' once, and then after some delay it will be repeatedly and quickly typed again until it is released. (This isn't exactly how the documentation describes it - I'm not sure if it was implemented without regard to key repeat, or if perhaps the documentation should be fixed).
danpost danpost

2013/5/9

#
What value is it always returning? Try adding a System.out.println statement in your act method as follows:
public void act()
{
    String str = Greenfoot.getKey();
    if (str != null) {
        System.out.println(str);
        for (Object obj : getObjects(Block.class)) {
            ((Block) obj).checkKey(str);
        }
    }
}
This should tell you when an actual key is being returned. I have places to go; people to meet. Should be back in 6 hours or so.
erdelf erdelf

2013/5/9

#
oh, I already tried this and it is almost immediatly. I will rewrite it to check the time difference Edit: it is returning a key every 32 or 33 Milliseconds I adapted the code to this, but it still seems to not work
        String str = Greenfoot.getKey();  
        if (str != null) 
        {  
            if(System.currentTimeMillis()-lastTime < 35)
            {
                System.out.println("close");
                if(lastString.equalsIgnoreCase(str))
                {
                    System.out.println("too close");
                    return;
                }
            }
            lastString = str;
            lastTime = System.currentTimeMillis();
            for (Object obj : getObjects(Block.class)) {  
                ((Block) obj).checkKey(str);  
            }  
        }  
You need to login to post a reply.
1
2