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

2018/4/23

Why does this give me NullPointerException?

t_k_990 t_k_990

2018/4/23

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Space. The final frontier
 * 
 * @author Ty Kutcher
 * @version 0.1
 */
public class Space extends World
{
    public boolean one = false;
    public boolean two = false;
    public boolean three = false;
    public boolean four = false;
    public boolean five = false;
    public boolean six= false;
    public boolean seven = false;
    public boolean eight = false;
    public boolean nine = false;
    private int random;
    /**
     * Create space.
     */
    public Space()
    {
        super(960, 620, 1);
        addBlocks();
    }
    public int mX;
    public int mY;
    MouseInfo mouse = Greenfoot.getMouseInfo();
    public void checkMouse(){
         mX = mouse.getX(); //this is where the nullpointerexception occurs
         mY = mouse.getY(); //this is where the nullpointerexception occurs as well
         if(mX > 0 && mY > 0){
            getMousePressed();
         } 
    }
    public void act() {
         getPlanetsPressed();
         checkMouse();
    }
    public void getMousePressed(){
        if(Greenfoot.mouseClicked(this)){
            double mass = Greenfoot.getRandomNumber(100) + 20;
            int size = Greenfoot.getRandomNumber(200) + 20;
            int colorR = Greenfoot.getRandomNumber(255);
            int colorB = Greenfoot.getRandomNumber(255);
            int colorG = Greenfoot.getRandomNumber(255);
            double direction = Greenfoot.getRandomNumber(360);
            addObject(new Body (size, mass, new Vector(direction, 0.3), new Color(colorR, colorB, colorG)), mX, mY);
        }
    }
    private String[] ObSounds = {"3a#", "3a", "3b", "3c#", "3c", "3d#", "3d", "3e", "3f#", "3f", "3g#", "3g", "4c#", "4c", "4d#", "4d", "4e", "4f#", "4f", "4g"};
    public void randomPlanet(int n) {
        int numOfPlanets = n;
        for(int x = 0; x < numOfPlanets;x++){
            double mass = Greenfoot.getRandomNumber(100) + 20;
            int size = Greenfoot.getRandomNumber(200) + 20;
            int colorR = Greenfoot.getRandomNumber(255);
            int colorB = Greenfoot.getRandomNumber(255);
            int colorG = Greenfoot.getRandomNumber(255);
            double direction = Greenfoot.getRandomNumber(360);
            int xPos = Greenfoot.getRandomNumber(getWidth() - 200);
            int yPos = Greenfoot.getRandomNumber(getHeight() - 100);
        }
    }
    public void getPlanetsPressed() {
        if(Greenfoot.isKeyDown("0")){
            removeBodies();
        }
        if(Greenfoot.isKeyDown("1")){
            removeBodies();
            randomPlanet(1);
        } // use this heiarchy of code for all the number except for 0
         if(Greenfoot.isKeyDown("2") && two == false){
             removeBodies();
            randomPlanet(2);
        }
        if(Greenfoot.isKeyDown("3") && three == false){
            removeBodies();
            randomPlanet(3);
        } //after they are put into the world, gravity is not correct as soon as the planet is placed
         if(Greenfoot.isKeyDown("4") && four == false){
             removeBodies();
            randomPlanet(4);
        } //after they are put into the world, gravity is not correct as soon as the planet is placed
        if(Greenfoot.isKeyDown("5") && five == false){
            removeBodies();
            randomPlanet(5);
        } //after they are put into the world, gravity is not correct as soon as the planet is placed
         if(Greenfoot.isKeyDown("6") && six == false){
             removeBodies();
            randomPlanet(6);
        } //after they are put into the world, gravity is not correct as soon as the planet is placed
        if(Greenfoot.isKeyDown("7") && seven == false){
            removeBodies();
            randomPlanet(7);
        } //after they are put into the world, gravity is not correct as soon as the planet is placed
         if(Greenfoot.isKeyDown("8") && eight == false){
             removeBodies();
            randomPlanet(8);
        } //after they are put into the world, gravity is not correct as soon as the planet is placed
        if(Greenfoot.isKeyDown("9") && nine == false){
            removeBodies();
            randomPlanet(9);
        } //after they are put into the world, gravity is not correct as soon as the planet is placed
    }
    private void removeBodies(){
        removeObjects (getObjects(Body.class));
    }
    private void removeAllObjects()
    {
        removeObjects (getObjects(Actor.class));
    }
    public void addBlocks() {
        //top
        for(int g = 0; g < 20; g++){
            addObject (new edgeOfWorld(ObSounds[g]), 50*g + 20, 15);
        }
        //left
        for(int x = 0;x < 19; x++){
            addObject (new edgeOfWorld(ObSounds[x]), 14, 46 + 30*x);
        }
        //right
        for(int i = 0;i < 19; i++){
            addObject( new edgeOfWorld(ObSounds[i]), 945, 40 + 30*i);
        }
        //bottom
        for(int t = 0; t < 19; t++){
            addObject (new edgeOfWorld(ObSounds[t]), 25 + 51 * t, getHeight() - 15);
        }
    }


}
Vercility Vercility

2018/4/23

#
Swap lines 31 and 32
danpost danpost

2018/4/23

#
Vercility wrote...
Swap lines 31 and 32
That is not enough. Quite often, getMouseInfo will return a null value and a NullPointerException will ensue when calling getX or getY on it. After swapping the lines, place the following after the second one:
if (mouse == null) return;
You need to login to post a reply.