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

在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;
     }


我的问题是,当我在targetEnvironmentComboBox方法之前的commandListener类中获取actionPerformed()时,它会引发Null指针异常。如果删除该代码,则会在targetEnvironmentComboBox方法中获得**的disableUsedComponents(),它能够成功获取combobox

同样,如果我执行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/

10-09 05:16