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?
Thank you for your time.
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);
}
}
}

