NullPointerException用于动作侦听器中的实例化按钮 [英] NullPointerException for instantiated button in action listener
问题描述
以下代码是我从中获取nullpointer异常的程序的摘要。当我按下GUI上的添加按钮时,错误消息指向此行:
The following code is a snippet of a program I am getting a nullpointer exception from. When I press the "Add" button on the GUI, an error message pointing to this line:
buttonPanel.addButton.setEnabled(false);
。我猜测由于某些原因,addButton为空,尽管我在buttonPanel的构造函数中将其实例化了:
is displayed. I'm guessing that the addButton is null for some reason although I instantiated it in the constructor of buttonPanel:
addButton = new JButton("Add");
addButton.addActionListener(buttonListener);
为什么是空指针错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AddButtonListener.actionPerformed(AddButtonListener.java:21)
出现?当侦听器在buttonPanel类中进行编码时,程序可以正常运行且没有错误。预先感谢您的帮助!
appearing? When the listener is coded inside the buttonPanel class, the program runs fine with no errors. Thanks in advance for your help!
import java.awt.GridLayout;
import javax.swing.*;
public class ButtonPanel extends JPanel{
public JButton addButton,
editButton,
deleteButton,
acceptButton,
cancelButton,
exitButton;
public JPanel topPanel,
exitPanel;
private ParentFrame parentFrame;
public static String buttonStatus;
public ButtonPanel(ParentFrame parent){
parentFrame = parent;
buttonStatus = "idle";
//Create Buttons
AddButtonListener buttonListener = new AddButtonListener(parent);
addButton = new JButton("Add");
addButton.addActionListener(buttonListener);
editButton = new JButton("Edit");
deleteButton = new JButton("Delete");
acceptButton = new JButton("Accept");
cancelButton = new JButton("Cancel");
exitButton = new JButton("Exit");
//Manipulate Buttons
acceptButton.setEnabled(false);
cancelButton.setEnabled(false);
//Add to panels
topPanel = new JPanel();
topPanel.add(addButton);
topPanel.add(editButton);
topPanel.add(deleteButton);
topPanel.add(acceptButton);
topPanel.add(cancelButton);
exitPanel = new JPanel();
exitPanel.add(exitButton);
this.setLayout(new GridLayout(2,1));
this.add(topPanel);
this.add(exitPanel);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AddButtonListener implements ActionListener{
private ParentFrame myFrame;
private ButtonPanel buttonPanel;
public AddButtonListener(ParentFrame parent){
myFrame = parent;
buttonPanel = parent.buttonPanel;
}
@Override
public void actionPerformed(ActionEvent ae) {
buttonPanel.buttonStatus = "add";
buttonPanel.addButton.setEnabled(false);
buttonPanel.editButton.setEnabled(false);
buttonPanel.deleteButton.setEnabled(false);
buttonPanel.acceptButton.setEnabled(true);
buttonPanel.cancelButton.setEnabled(true);
}
}
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class ParentFrame extends JFrame{
public ButtonPanel buttonPanel;
public ParentFrame(){
this.setResizable(false);
buttonPanel = new ButtonPanel(this);
this.add(buttonPanel, BorderLayout.SOUTH);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 300);
}
public static void main(String[] args){
ParentFrame frame = new ParentFrame();
frame.setVisible(true);
}
}
推荐答案
您的buttonpanel要求引用要构造的 ParentFrame
。 ButtonPanel
正在父框架中构建并创建一个侦听器,该侦听器引用父框架的按钮面板。
Your buttonpanel requires a reference to ParentFrame
to be constructed. ButtonPanel
is being constructed in parent frame and creating a listener, which references the parent frame's button panel.
不幸的是,此时尚未分配按钮面板,因此您的操作侦听器为其按钮面板实例分配了空值。
Unfortunately, the button panel hasn't been assigned at that point, and so your action listener has a null value assigned to its instance of button panel.
我认为问题在于您的 AddButtonListener
中的按钮面板实例为空。
I think the problem is your button panel instance is null in your AddButtonListener
.
您可以通过将ButtonPanel实例传递到您的<$中来解决此问题。 c $ c> AddButtonListener 构造函数。由于 AddButtonListener
没有使用 ParentFrame
,所以根本不用传递它。
You can fix this by passing a ButtonPanel instance into your AddButtonListener
constructor. As AddButtonListener
isn't using ParentFrame
, don't bother passing it at all.
private ButtonPanel buttonPanel;
public AddButtonListener(ButtonPanel panel){
myFrame = parent;
buttonPanel = panel;
}
然后在按钮面板构造函数中:
And then in your button panel constructor:
public ButtonPanel(ParentFrame parent){
parentFrame = parent;
buttonStatus = "idle";
//Create Buttons
AddButtonListener buttonListener = new AddButtonListener(this);
//rest the code
此外,您不应该构建这样的结构。您正在做的是将 ButtonPanel
与 ParentFrame
紧密耦合。这意味着,如果您的父框架发生更改,则可能导致 ButtonPanel
发生另一次更改,从而减少了可维护的代码。
Aside, you shouldn't be structuring things like this. What you're doing is tightly coupling your ButtonPanel
with your ParentFrame
. This means if your parent frame changes it could cause another change in ButtonPanel
, which makes for less maintanable code.
这篇关于NullPointerException用于动作侦听器中的实例化按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!