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:

protected void onDraw(Canvas canvas) {

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)
  • 1
    \$\begingroup\$ From looking at the code my guess would be that block=null, e.g. it's not assigned an object. \$\endgroup\$
    – Matsemann
    Commented Jun 7, 2012 at 21:47
  • \$\begingroup\$ I would change question title, before clicking on it I was thinking problem was the final app file size being too big. \$\endgroup\$
    – tigrou
    Commented Jun 8, 2012 at 12:31
  • \$\begingroup\$ what do you think i should change it too? \$\endgroup\$ Commented Jun 8, 2012 at 12:32
  • \$\begingroup\$ mabye Android Game Development problem with speed = distance / time ?? \$\endgroup\$ Commented Jun 8, 2012 at 12:33

2 Answers 2


Is your block object previously created?


In MainGame code, there is no need to round as currently written. The result of an int divided by an int is an int. Instead, cast to double by dividing by 100.0 and the round by adding 0.5 and casting result to an int

For example:

int screenWidth = 150;
int blockSpeed = screenWidth/100;
System.out.println("way #1 before round, blockSize is "+blockSpeed);
System.out.println("way #1 after round, blockSize is "+blockSpeed);
blockSpeed = (int)((screenWidth/100.0)+0.5);
System.out.println("way #2, blockSize is "+blockSpeed);


way #1 before round, blockSize is 1
way #1 after round, blockSize is 1
way #2, blockSize is 2
  • \$\begingroup\$ You should look into the markdown formatting guide and get clearer on how you can format your posts with it. :) \$\endgroup\$ Commented Jun 7, 2012 at 22:48
  • \$\begingroup\$ The problem is not at the math.round,if i just put a code in which says 'block.speed = 18;' it still tells be to force close. Il update my post with my error log. \$\endgroup\$ Commented Jun 8, 2012 at 12:27
  • \$\begingroup\$ The error log is telling you that you've got a null pointer on line 318 of MainGame.java. "block" is likely null...did you initialize it in setup? \$\endgroup\$
    – amb
    Commented Jun 8, 2012 at 14:09
  • \$\begingroup\$ what do you mean initialize it in setup? \$\endgroup\$ Commented Jun 8, 2012 at 14:33
  • \$\begingroup\$ in your onCreate method do you create a new block object? \$\endgroup\$
    – amb
    Commented Jun 8, 2012 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .