1

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.

            }
        }
    }

}

3 Answers 3

3

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...

Sign up to request clarification or add additional context in comments.

Comments

2

You have to initialize your floorButton.

    floorButton = new JButton [yourCount];

4 Comments

You'll still end up with a NullPointerException - a lot going on here ;)
You just created your JButton array object on heap. You further need to create JButton objects by calling new JButton(). @MadProgrammer: ;)
@FlorescentTicker I was referring to the OP's code, creating the array will solve the first NullPointerException, but based on the OP's code, won't solve the second one...it's a little unobvious ;)
@MadProgrammer Yes, you are correct. When I was watching to the OP's code, this was the first problem what I see. So, I did not watch to the next lines...I know, my fail :-(
1

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)); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.