Android Game Development problem with Speed = Distance / Time
        Posted  
        
            by 
                Charlton Santana
            
        on Game Development
        
        See other posts from Game Development
        
            or by Charlton Santana
        
        
        
        Published on 2012-06-07T20:30:17Z
        Indexed on 
            2012/06/08
            16:50 UTC
        
        
        Read the original article
        Hit count: 380
        
I have been coding speed for an object. I have made it so the object will move from one end of the screen to another at a speed depending on the screen size, at the monemt I have made it so it will take one second to pass the screen. So i have worked out the speed in code but when I go to assign the speed it tells me to force close and i do not understand why. Here is the code:
MainGame Code:
@Override
protected void onDraw(Canvas canvas) {
    setBlockSpeed(getWidth());
}
private int blockSpeed;
 private void setBlockSpeed(int screenWidth){
Log.d(TAG, "screenWidth " + screenWidth);
 blockSpeed = screenWidth / 100; // 100 is the FPS.. i want it to take 1 second to pass the screen
 Math.round(blockSpeed); // to make it a whole number   
 block.speed = blockSpeed; // this is line 318!! if i put eg block.speed = 8; it still tells me to force close  
 }
Block.java Code:
public int speed;
public void draw(Canvas canvas) {
    canvas.drawBitmap(bitmap, x - (bitmap.getWidth() / 2), y - (bitmap.getHeight() / 2), null);
    if(dontmove == 0){
        this.x -= speed; // if it was eg this.x -= 18; it would not have an error
    }
}
The exception
06-08 13:22:34.315: E/AndroidRuntime(2801): FATAL EXCEPTION: Thread-11
06-08 13:22:34.315: E/AndroidRuntime(2801): java.lang.NullPointerException
06-08 13:22:34.315: E/AndroidRuntime(2801):     at com.charltonsantana.game.MainGame.setBlockSpeed(MainGame.java:318)
06-08 13:22:34.315: E/AndroidRuntime(2801):     at com.charltonsantana.game.MainGame.onDraw(MainGame.java:351)
06-08 13:22:34.315: E/AndroidRuntime(2801):     at com.charltonsantana.game.MainThread.run(MainThread.java:64)
        © Game Development or respective owner