【问题标题】:Null Pointer with JButton Array带有 JButton 数组的空指针
【发布时间】:2014-05-22 01:21:13
【问题描述】:

到目前为止,我已经在添加我的 ControlPanel 类时创建了一个框架,该类采用一个 int,并且该 int 用于用添加到面板并显示的 JButton 对象填充数组。

但是,当我尝试将 JButtons 添加到数组时出现空指针错误。我不知道为什么(因为我认为空指针来自当您尝试引用数组中不存在的东西时?)

对此的任何帮助将不胜感激。

错误信息:

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

主类

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


    }

}

控制面板类:

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.

            }
        }
    }

}

【问题讨论】:

    标签: java arrays swing nullpointerexception jbutton


    【解决方案1】:

    floorButton 没有初始化为任何东西...

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

    初始化数组以反映您需要的内容,另外,不要使用floorButton[i].add,您不想将按钮添加到(当前是null 元素)按钮,您想将其分配给数组的位置...

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

    我猜你会想对floorIn做同样的事情...

    【讨论】:

      【解决方案2】:

      您创建了 JButton 数组,但并未创建数组中的每个元素。为此:

      替换这个:

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

      通过这个:

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

      【讨论】:

        【解决方案3】:

        你必须初始化你的floorButton

            floorButton = new JButton [yourCount];
        

        【讨论】:

        • 你仍然会得到一个NullPointerException - 这里发生了很多事情;)
        • 您刚刚在堆上创建了 JButton 数组对象。您还需要通过调用 new JButton() 创建 JButton 对象。 @MadProgrammer:;)
        • @FlorescentTicker 我指的是OP的代码,创建数组将解决第一个NullPointerException,但基于OP的代码,不会解决第二个......这有点不明显;)
        • @MadProgrammer 是的,你是对的。当我查看 OP 的代码时,这是我看到的第一个问题。所以,我没有看下一行……我知道,我的失败:-(
        猜你喜欢
        • 1970-01-01
        • 2014-01-24
        • 2013-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-13
        • 2011-07-25
        相关资源
        最近更新 更多