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

2023/11/19

problem transferring variable

btheys btheys

2023/11/19

#
I am working on a dialoguebox with a working "next" button that changes the line of text being shown, everytime it's clicked. In the "Nextbutton" class, i have managed to make a variable where if you click the next button, a counter goes up. The problem arises when i try to bring that variable from my 'Nextbutton' class over to 'Dialoguebox' class. I already made a string containing the script. When i try to start the game it gave me a "java.lang.NullPointerException", so i did some experimenting the world class but i can't wrap my head around it, can someone please help me? code Nextbutton:
public class Nextbutton extends Scriptje
{
    public boolean mouseDown;
    public int currentline;
    private GreenfootImage image;
    public boolean lineChanged = false;
    /**
     * Act - do whatever the Nextbutton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Nextbutton()
    {
         mouseDown = false;
         verkleinButton();
         image = getImage();
    }
    
    public void act()
    {
        if (!mouseDown && Greenfoot.mouseClicked(this))
        {
            currentline++;
            getWorld().showText("currentline: " + currentline, 200, 100); //verwijder dit zodra dat het script werkt
            lineChanged = true;
        }
    }
    
    private void verkleinButton()
    {
        GreenfootImage img = new GreenfootImage("nextbutton.png");
        img.scale(img.getWidth()/10, img.getHeight()/10);
        setImage(img);
    }
}
code Dialoguebox:
public class Dialoogbox extends Scriptje
{
    public String[] lines={"line 1", "line 2", "line 3?", "line 4"};
    private int followscript;
    public boolean scriptChanged = false;
    /**
     * Act - do whatever the Dialoogbox wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    // dialoogbox (klasse van knopje)
    public Dialoogbox()
    {
        followscript = getWorld().getObjects(Nextbutton.class).get(0).currentline;
        scriptChanged = getWorld().getObjects(Nextbutton.class).get(0).lineChanged;
        
    }  

    public void script(){
        if (!scriptChanged)
        {
            GreenfootImage image = new GreenfootImage(lines[followscript], 24, Color.BLACK, new Color(255,255,255));
        }
    }

    public void act()
    {

    }
}
code world:
public MyWorld()
    {    
        super(600, 400, 1);
        prepare();
        setPaintOrder(Nextbutton.class, Dialoogbox.class, Verkoper.class, Klant.class);
        setBackground("wereldkaart.jpg");
        toonTekst("Kies het continent dat je wilt bezoeken:", getWidth() / 2, getHeight() / 4);
        Nextbutton knopje = new Nextbutton();
        Dialoogbox boxje = new Dialoogbox(knopje);
    }
Super_Hippo Super_Hippo

2023/11/19

#
The constructor of a class is called when an object of that class is created (for example “new Dialoogbox(...)”). At that point, it isn’t in a World and “getWorld()” returns null.
You need to login to post a reply.