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

2018/1/6

Getting a nullPointerException and random turning problems

Dimak Dimak

2018/1/6

#
Hello everyone, I am having trouble with a random exception error. This class gets spawned by an asteroid after it gets hit by a bullet. The problem I am having is sometimes after it decays (random int between 1 and 50) it shows a nullPointerException error, but it does not happen all the time. My question is how do I fix it from happening? Another bug I am having is with the turning. The rotation (random int between 1 and 360) is being fed into the turn function provided by greenfoot. Even though the rotation int is indeed a random number between 1 and 360 ( I checked in debug) it only appears to have 8 or so possible rotations and end up forming a grid pattern ( I cannot provide an image because the last post got my account banned because of posting a link) Is the problem with the Debris image being too small? (2x2 pixels) or is there a way to fix this?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;
/**
 * Write a description of class Debris here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Debris extends Actor
{
    World MyWorld = getWorld();
    Random randomR = new Random();
    private int speed = 0;
    
    
    
    int decay;
    public Debris()
    {
        int rotation = randomR.nextInt(360)+1;
        decay = (int) ((Math.random() * 50));
        speed = (int) (1+ Math.random() * 2);
        turn(rotation);
    }
    public void act() 
    {
        
        
        move(speed);
        destroyAtEdge();
        if (decay != 0)
        {
            decay--;
        }
        else
        {
            getWorld().removeObject(this);
        }
        
        
    }
    public void destroyAtEdge()
    {
        int y = getY();
        int x = getX();
        World myWorld = getWorld();
        if (y == 0 )
        {
            getWorld().removeObject(this);
        }
        else if ( x==0 )
        {
            getWorld().removeObject(this);
        }
        else if (y == myWorld.getHeight() - 1)  //   -1 because it would not find it otherwise
        {
            getWorld().removeObject(this);
        }
        else if ( x == myWorld.getWidth() - 1)   //   -1 because it would not find it otherwise
        {
            getWorld().removeObject(this);
        }
    }

 
}
Thank you for your time.
Super_Hippo Super_Hippo

2018/1/6

#
You get the nullpointer exception when you try to execute line 37 if the object got removed from the world from the destroyAtEdge object. To solve this, you could move line 30 to right after 33. If you move with a speed of 1, you only have 8 different movement directions. The object starts in the middle of cell, moves in its direction and is then placed in the middle of the cell which is belongs to then. If you don't save the exact values (double instead of int for x and y values --> you can try the SmoothMover class), you can't have other possible movements with a speed of 1. Higher speed leads to a higher number of possible rotations, but it still won't be accurate without using doubles. There is no need to use the Random class. Greenfoot has its own method to get a random number, the Greenfoot.getRandomNumber method.
Dimak Dimak

2018/1/6

#
Thank you for your help, I will try to implement a SmoothMover using this scenario and hopefully it turns out good. Thank you for your time.
danpost danpost

2018/1/6

#
Super_Hippo wrote...
If you don't save the exact values (double instead of int for x and y values --> you can try the SmoothMover class), you can't have other possible movements with a speed of 1. Higher speed leads to a higher number of possible rotations, but it still won't be accurate without using doubles.
That is not exactly true. My QActor class uses int values for both location and rotation (in coordination with a "Zoom" factor) to create quite accurate smooth movement. With a "zoom" of 100, each int unit is 1/100 of a cell or 1/100 of a degree. Int values are much quicker to operate on in terms of CPU usage and therefore helps to minimize lag.
You need to login to post a reply.