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

2021/6/19

Help idk whats wrong with this

mariq_rasyid29 mariq_rasyid29

2021/6/19

#
that is from the terminal
java.lang.NullPointerException
	at Pup2.addBlizzard(Pup2.java:36)
	at Pup2.act(Pup2.java:58)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
and here the pup 2 code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Pup2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pup2 extends Puppy
{public int timer=450;
    public String[] dialogue4=
        {"Pup 1 & Pup 2 : ''Hello, Alpha. ^^''",
            "Blizzard : ''Hi, Kiddo. :)",
            "Pup 1 : ''Alpha, Where are you going?",
            "Blizzard : ''I'm go to Ancient Dugeons :)",
            "Pup 2 : ''Woah, Are you sure Alpha? \n That place is very Dangerous,\n My Parent said only Alpha can to there.",
            "Blizzard : ''Im sure about that :)",
            "Pup 1 & Pup 2: ''Ok Then Alpha, Good luck.''",
            "Blizzard : ''Thank you, Kindos. See ya ;)''",
            "Pup 1 & Pup 2 : ''Your Welcome, Alpha, see yaa!!!''",
        };
    public void touch()
    {
        if(isTouching(Blizzard.class))
        {
            adddialog();
        }
        removeTouching(Blizzard.class);

    } 

    private void addBlizzard()
    {if (--timer == 0) 
        {
            getWorld().removeObject(this);
            getWorld().removeObjects(getWorld().getObjects(Pup1.class));
            Blizzard bl = new Blizzard();
            getWorld().addObject(bl, 696, 432);   
        }

    }

    public void adddialog()
    { Dialogue dialogue = new Dialogue(dialogue4);
        getWorld().addObject(dialogue,230, 56);

    }

    private void dialogcommand()
    {if (!getWorld().getObjects(Dialogue.class).isEmpty() && "z".equals(Greenfoot.getKey()))
        {
            ((Dialogue)getWorld().getObjects(Dialogue.class).get(0)).nextText();
        }
    }

    public void act()
    {touch(); 
        dialogcommand();
        addBlizzard();
    }
}
but in editor said "class compiled-no syntax errors"
Gbasire Gbasire

2021/6/19

#
in your addBlizzard method, you remove the object so it can't do the following things. remove the object at the end like this :
 private void addBlizzard()
    {
    if (--timer == 0) 
        {
            getWorld().removeObjects(getWorld().getObjects(Pup1.class));
            Blizzard bl = new Blizzard();
            getWorld().addObject(bl, 696, 432);
            getWorld().removeObject(this);
        }
    }
Super_Hippo Super_Hippo

2021/6/19

#
Alternatively, you can get and save a reference to the world before the object was removed. Then the order doesn’t matter:
World w = getWorld();
w.removeObject(this);
w.removeObjects(w.getObjects(Pup1.class));
w.addObject(new Blizzard(), 696, 432);
mariq_rasyid29 mariq_rasyid29

2021/6/19

#
thx all for help me ^^
You need to login to post a reply.