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

2018/12/2

Error in Winning screen

Tommes Tommes

2018/12/2

#
I am getting the error NullPointerException, if the ball is going under 5 / 795. And cant find it. this is my actor ball: private int Beschleunigung = 0; private int H = 1; private int G = 1; /** * Act - do whatever the ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { schläger(); wand(); speed(); destroy(); } private void schläger() { if (isTouching(schläger.class) && H == 1) { setRotation(180-getRotation()+(Greenfoot.getRandomNumber(10) -Greenfoot.getRandomNumber(20))); Beschleunigung = Beschleunigung +1; H = 0; G = 1; } if (isTouching(schläger_2.class) && G==1) { setRotation(180-getRotation()+(Greenfoot.getRandomNumber(10) -Greenfoot.getRandomNumber(20))); Beschleunigung = Beschleunigung +1; G = 0; H = 1; } } private void wand() { if (isAtEdge()) { setRotation(360-getRotation()); } } private void speed() { if (Beschleunigung <= 5) { move(5); } if (Beschleunigung > 5) { move(6); } if (Beschleunigung > 10) { move(7); } if (Beschleunigung > 15) { move(8); } if (Beschleunigung > 20) { move(9); } } private void destroy() { if (getX() >= 795 && getWorld() != null) { getWorld().removeObject(this); getWorld().addObject(new Red_wins(), 400, 300); Greenfoot.stop(); } if (getX() <= 5 && getWorld() != null) { getWorld().removeObject(this); getWorld().addObject(new Green_wins(), 400, 300); Greenfoot.stop() } } }
Tommes Tommes

2018/12/2

#
Oh sorry
private int Beschleunigung = 0;
    private int H = 1;
    private int G = 1;
    /**
     * Act - do whatever the ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        schläger();
        wand();
        speed();
        destroy();
    }
    private void schläger()  {
        if (isTouching(schläger.class) && H == 1) {
         setRotation(180-getRotation()+(Greenfoot.getRandomNumber(10)
         -Greenfoot.getRandomNumber(20)));
         Beschleunigung = Beschleunigung +1;
         H = 0;
         G = 1;
        }
        if (isTouching(schläger_2.class) && G==1)  {
         setRotation(180-getRotation()+(Greenfoot.getRandomNumber(10)
         -Greenfoot.getRandomNumber(20)));
         Beschleunigung = Beschleunigung +1;
         G = 0;
         H = 1;
        }
    }
    private void wand()  {
        if (isAtEdge())  {
         setRotation(360-getRotation());
        }
    }
    private void speed()  {
        if (Beschleunigung <= 5)  {
            move(5);
        }
        if (Beschleunigung > 5) {
            move(6);
        }
        if (Beschleunigung > 10) {
            move(7);
        }
        if (Beschleunigung > 15)  {
            move(8);
        }
        if (Beschleunigung > 20)  {
            move(9);
        }
    }
    private void destroy()  {
        if (getX() >= 795 && getWorld() != null)  {
            getWorld().removeObject(this);
            getWorld().addObject(new Red_wins(), 400, 300);
            Greenfoot.stop();
        }
        if (getX() <= 5 && getWorld() != null)  {
            getWorld().removeObject(this);
            getWorld().addObject(new Green_wins(), 400, 300);
           Greenfoot.stop();
        }
    }
}
nccb nccb

2018/12/2

#
When you call getWorld().removeObject(this), the actor is removed from the world, and thereafter getWorld() will return null, including on the following line (twice in destroy) where you call addObject. Easiest fix is to add the new object before removing yourself (i.e. swap the ordering of the two lines), another alternative is to store getWorld() in a variable before making those calls using that world variable.
Tommes Tommes

2018/12/2

#
Thank you It works perfectly :)
Tommes Tommes

2018/12/2

#
Okay, it doesn't work perfect. I was wrong. I changed the order of the lines like u said, but now the one side works and the other got still the error.
Super_Hippo Super_Hippo

2018/12/2

#
The "getWorld()!=null" doesn't really do its job there. If it is null, then the line gives you an error because "getX" is executed which doesn't work when the object isn't in a world. Either change that ordering and/or add an "else" before the second "if" in the destroy method.
Tommes Tommes

2018/12/2

#
Thank you for the explanation. Now it works.
You need to login to post a reply.