Question

So far I have created a frame at adds my ControlPanel class, the class takes an int, and that int is used to fill an array with JButton objects which are the added to a Panel and displayed.

However I am getting a Null Pointer error when I try to add JButtons to the array. I'm not sure why (since I thought null pointers were from when you try to reference something that isn't in the spot in the array?)

Any help on this would be great appreciated.

Error Message:

Exception in thread "main" java.lang.NullPointerException
    at elevator.ControlPanel.<init>(ControlPanel.java:22)
    at elevator.Elevator_Simulator.main(Elevator_Simulator.java:27)

Main Class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;


public class Elevator_Simulator extends JFrame {




    public static void main(String[] args) {


        JFrame frame = new JFrame ("Elevator Simulation");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ControlPanel(8));
        frame.pack();
        frame.setVisible(true);


    }

}

Control Panel Class:

import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;

public class ControlPanel extends JPanel implements ActionListener {

    public JButton[] floorButton; // an array of floor buttons.
    public int[] floorIn;          // state of button. 0 == not click >= 1 means clicked (how many times).

    public ControlPanel(int x) {

        JPanel floors = new JPanel();

        for (int i = 0; i < x; i++) { // interates through user input and creates that many JButtons. 
            floorButton[i].add(new JButton("F" + Integer.toString(i))); // adds a button to button array. Sets text to F(floorNo).
            floors.add(floorButton[i]); // adds buttons to the floor panel.
            floorButton[i].addActionListener(this); // adds action listener to the buttons.
        }
    }

    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < floorButton.length; i++) {
            if (e.getSource() == floorButton[i]) { // checks which button the event occured on.

                floorButton[i].setBackground(Color.red); // sets button background to red.
                floorButton[i].setText(floorButton[i].getText() + "(1)"); // marks button as clicked.

            }
        }
    }

}
Was it helpful?

Solution

floorButton is not initalised to anything...

public JButton[] floorButton; // I'm null

public ControlPanel(int x) {
    //...
    for (int i = 0; i < x; i++) { // in
        // Still null
        floorButton[i].add(new JButton("F" + Integer.toString(i))); 

Initalise the array to reflect what you need, also, don't use floorButton[i].add, you don't want to add the button to (what is currently a null element) button, you want to assign it to the position of the array...

public ControlPanel(int x) {
    //...
    floorButton = new JButton[x];
    for (int i = 0; i < x; i++) { // in
        floorButton[i] = new JButton("F" + Integer.toString(i)); 

I'm guessing you'll want to do the same with floorIn...

OTHER TIPS

You have to initialize your floorButton.

    floorButton = new JButton [yourCount];

You created the array of JButton, but you didn't create every element in the array. To do this:

Replace this:

 floorButton[i].add(new JButton("F" + Integer.toString(i)));

by this:

 floorButton[i] = new JButton("F" + Integer.toString(i)); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top