注意:这是我的实际代码的缩短版本,但在结构方面几乎相同。我基本上切断了连接到组件代码的面板和连接到框架代码的面板。
在 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()
时,它会抛出空指针异常。如果我删除了targetEnvironmentComboBox
方法中的disableUsedComponents()
**,那么它就能成功获得combobox
。
此外,如果我执行executeButton
侦听器,它能够获取CommandListener
类中所有组件的引用,但如果我执行getCommandOptionsComboBox
的侦听器,它将返回null executeButton
和targetEnvironmentComboBox
。
任何人都可以解释为什么会这样吗?
其次,我知道这可能不是最好的实现。关于我可以改变以遵循更好的做法的任何建议吗?
答案 0 :(得分:2)
您正在获得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 */
}
然后传递这些字段(确保你传递非空值),并取消所有静态变量(或重构为更易于管理的东西)。
答案 1 :(得分:1)
不使用static
方法访问实例,而是使用封装并通过setters and getters
访问它。
获得targetEnvironmentComboBox
对象的实例的唯一方法是让您的展示实例同时指向&#34;在内存中的空间,尝试:
public JComboBox getComobo(){
return [your_object];
}
而不是:
public static JComboBox getTargetEnvironmentComboBox(){
// your code . . .
}
并通过Display
构造函数传递CommandListener
实例。
答案 2 :(得分:-2)
这是一个简化的例子,说明如何设置这种方式,既可以大大减少样板代码的数量,又基本上不受您遇到的初始化问题的影响。这也是Swing UI代码中一种相当典型的方法,实际上也是引入匿名内部类的驱动因素之一。
public class Display extends JPanel {
private JButton executeButton;
private JComboBox<String> commandOptionsComboBox;
public Display() {
executeButton = new JButton("Exec");
commandOptionsComboBox = new JComboBox<>();
executeButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Action performed");
commandOptionsComboBox.setEnabled(false);
}
});
}