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

2014/10/22

UILibrary scenario question

murphys murphys

2014/10/22

#
I am trying to work with Mik's UILibrary scenario. Specifically I am trying to access the value of the switch from within the UIDemo class. The following code compiles but then produces a runtime error with a null pointer exception:
 public void act(){
        
       boolean test = onOff.isOn();
    }
Please could you explain why this error occurs and what I need to do to access the value of the switch state. Many thanks in advance.
danpost danpost

2014/10/22

#
The only thing in the code given that could possibly be null is 'onOff'. Make sure you are setting an object to that field. If you believe you are setting an object to it, post the code that declares the field and the code that sets an object to it.
murphys murphys

2014/10/22

#
The code is below:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A demo of some GUI objects (from the "MSG" Greenfoot GUI toolkit).
 * 
 * @author mik 
 * @version 0.1
 */
public class UIDemo extends World
    implements ButtonListener
{
    private Label label1;
    private Label label2;
    private int clickCount;
    public Switch onOff;
    public boolean density = true;
    
    /**
     * Constructor for objects of class Demo.
     * 
     */
    public UIDemo()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        getBackground().setColor(new Color(200, 220, 210));
        getBackground().fill();
        prepare();
    }

    /**
     * A button in the world was clicked.
     * 
     * @param button  The button object that was clicked.
     */
    public void buttonClicked(Button button)
    {
        Label label;
        if (button.getIDNumber() == 1) {
            label = label2;
        }
        else {
            label = label1;
        }
            
        clickCount++;
        if (clickCount == 4) {
            label.setText("click");
            clickCount = 0;
        }
        else {
            label.setText(label.getText() + ", click");
        }
    }
    
    /**
     * Prepare the world for the start of the program. That is: create the initial objects and add them to the world.
     */
    public void prepare()
    {
        // a normal button - the size adjusts to the text length
        Button button = new Button("Simple Button");
        addObject(button, 20, 40);
        
        // a button with fixed width and height
        Button button2 = new Button("A button with custom size", 1);
        button2.setWidth(250);
        button2.setHeight(100);
        addObject(button2, 20, 80);
        
        // a normal label - by default, the background is transparent
        label1 = new Label("A simple text label");
        addObject(label1, 300, 40);
        
        // a label with some customisation
        label2 = new Label("Fancy label");
        label2.setBackground(Color.WHITE);
        label2.setForeground(Color.DARK_GRAY);
        label2.setBorder(Color.GRAY);
        label2.setWidth(200);
        label2.setAlignment(Label.RIGHT);
        addObject(label2, 300, 80);
        
        // a switch
        Switch onOff = new Switch("On", "Off");
        addObject(onOff, 300, 140);
        
        // a checkbox
        Checkbox box = new Checkbox("with Cream");
        addObject(box, 30, 220);
        box = new Checkbox("with Sugar");
        box.setChecked(true);
        addObject(box, 30, 240);
        box = new Checkbox("extra strong");
        addObject(box, 30, 260);

        // a slider
        Slider slider = new Slider("Shift", 0, 25);
        addObject(slider, 300, 260);
        
    }
    
    public void act(){
        
       boolean test = onOff.isOn();
    }
    

}
danpost danpost

2014/10/22

#
You are never assigning an object to the field 'onOff' defined on line 16. On line 86, you are creating a local variable called 'onOff', which is not the same as the field defined on line 16, and assigning it an object. After line 86, add the following line:
this.onOff = onOff;
Or, you can just change line 86 to this:
onOff = new Switch("On", "Off");
Your 'label1' and 'label2' fields have the same issue.
murphys murphys

2014/10/22

#
Thank you. The code now works as expected.
You need to login to post a reply.