java - 为什么我的 JComboBox 返回 null?

标签 java swing nullpointerexception

注意:这是我的实际代码的缩短版本,但在结构方面几乎相同。我基本上剪掉了连接到组件代码的面板和连接到框架代码的面板。

Display.java 上,我有以下内容。请注意,我没有向 targetEnvironmentComboBox 添加任何监听器。不确定这是否会成为问题:

public class Display extends JFrame {

    private static JButton executeButton;
    private static JComboBox<String> commandOptionsComboBox, targetEnvironmentComboBox;

    //getters
    public static JButton getExecuteButton()    {   return executeButton;               }
    public static JComboBox<String> getCommandOptionsComboBox()    {    return commandOptionsComboBox;      }
    public static JComboBox<String> getTargetEnvironmentComboBox()      {   return targetEnvironmentComboBox;   }

    public Display() {
          super("Display");
          setLayout(new BorderLayout());
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          commandOptionsComboBox = new JComboBox(commandOptions.toArray());
          commandOptionsComboBox.addActionListener(new CommandListener());

          executeButton = new JButton("Execute");
          executeButton.addActionListener(new CommandListener());

          targetEnvironmentComboBox = new JComboBox(targetEnvironments.toArray());


        //main method that gets executed at the start of program
        public static void main(String[] args) {
        new Display();

        }
  }

在单独的CommandListener.java上,我有以下内容:

 public class CommandListener implements ActionListener {

JButton executeButton = Display.getExecuteButton();
JComboBox<String> commandOptionsComboBox = Display.getCommandOptionsComboBox();
JComboBox<String> targetEnvironmentComboBox = Display.getTargetEnvironmentComboBox();

@Override
public void actionPerformed(ActionEvent event) {

    if(event.getSource() == executeButton) {
        System.out.println("HGello world");
        executeCommand(event);

    }else if (event.getSource() == commandOptionsComboBox) {
        System.out.println("commandline");
        disableUnusedComponents(event);
    }
}

private void disableUnusedComponents(ActionEvent event) {
     **JComboBox<String> targetEnvironmentComboBox = Display.getTargetEnvironmentComboBox();**
     String command = (String) commandOptionsComboBox.getSelectedItem();
    switch(command) {
          case "-duplicate":
            targetEnvironmentComboBox.setEnabled(false);
            targetEnvironmentComboBox.setVisible(false);
            break;
          default: break;
     }

我的问题是,当我在 actionPerformed() 方法之前的 commandListener 类中获取 targetEnvironmentComboBox 时,它会抛出一个 空指针异常。如果我删除它,我会在 disableUsedComponents() 方法中获取 targetEnvironmentComboBox,其中 **,它能够成功获取组合框

此外,如果我执行 executeButton 监听器,它就能够获取对 CommandListener 类中所有组件的引用,但是如果我执行 >getCommandOptionsComboBox,它为 executeButtontargetEnvironmentComboBox 返回 null。

谁能解释一下为什么会这样?

其次,我知道这可能不是最好的实现。对于我可以改变以遵循更好的实践的事情有什么建议吗?

最佳答案

您收到 null,因为 targetEnvironmentComboBox 直到您尝试在 CommandListener 中访问之后后才会初始化。当创建新的 CommandListener 时,它会读取 targetEnvironmentComboBox 并将其存储到本地变量中。并查看创建 CommandListener 的位置:

//Creating CommandListeners, which take the reference to targetEnvironmentComboBox (null)
//Although targetEnvironmentComboBox is later set to a usable value, the local copies still
//have the null reference which is assigned here
commandOptionsComboBox.addActionListener(new CommandListener());
executeButton = new JButton("Execute");
executeButton.addActionListener(new CommandListener());

//initializing targetEnvironmentComboBox, which is still null (the first time at least)
targetEnvironmentComboBox = new JComboBox(targetEnvironments.toArray());

如果将 Display.getTargetEnvironmentComboBox() 放入该方法内,则在需要时不会获取引用的副本,此时它已正确初始化。

一个简单的解决方法,虽然不是正确的方法,但首先初始化它:

targetEnvironmentComboBox = new JComboBox(targetEnvironments.toArray());

commandOptionsComboBox.addActionListener(new CommandListener());
executeButton = new JButton("Execute");
executeButton.addActionListener(new CommandListener());

更好的解决方案是使用适当的封装,并将构造函数中访问正确对象所需的内容传递给 CommandListener。我建议修改您的 CommandListener 类,如下所示:

public class CommandListener implements ActionListener {
    JButton executeButton;
    JComboBox<String> commandOptionsComboBox;
    JComboBox<String> targetEnvironmentComboBox;

    public CommandListener(JButton executeButton,
                           JComboBox<String> commandOptionsComboBox,
                           JComboBox<String> targetEnvironmentComboBox){
        this.executeButton = executeButton;
        this.commandOptionsComboBox = commandOptionsComboBox;
        this.targetEnvironmentComboBox = targetEnvironmentComboBox;
    }

    /* The rest of your code */
}

然后传递这些字段(确保传递非空值),并消除所有静态变量(或重构为更易于管理的内容)。

关于java - 为什么我的 JComboBox 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34550917/

相关文章:

java - 如何使用泛型将一个 RecyclerView 适配器用于不同类型的对象?

java - 图的 HashMap 表示

java - 学习 Java 和 Solr 的优秀 Java 资源

android - 从 MediaStore.Images.Media.DATA 获取图像的方向

带有大量图像的android java.lang.OutOfMemoryError recyclerview

java - 将 'dryRun' 设置为 true 时,BigQuery 客户端库抛出 NPE

java - 如何在不舍入的情况下在 Java 中截断小数?

java - 使用键盘 ("AWT-EventQueue-0")

java - 在 Java Swing 游戏循环中正确使用 SwingWorker

java - Thread 中的 repaint() 导致 CPU 使用率很高