我已经将ActionListener实现为我的主类的内部类,并且我试图在该类的实例上调用其中一个主类方法。但是它继续在ActionListener中的方法调用上返回NullPointerException。
提供的代码。
尝试点击" RefreshButton"按预期将文本附加到TextArea,但随后返回 "线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException 在frontend.MainGUI $ RefreshListener.actionPerformed(MainGUI.java:104)"
指向gui.testing();在RefreshListener中。我可能错过了一些简单的东西,但它给了我一些麻烦。
public class MainGUI {
JFrame MainFrame;
JLabel MainLabel;
JLabel FooterLabel;
JTextArea ListText;
MainGUI gui;
public static void main (String[] args) {
MainGUI staticGui = new MainGUI();
staticGui.begin();
} // Close Main
public void begin() {
gui = new MainGUI();
gui.testing();
gui.go();
}
public void go() {
MainFrame = new JFrame();
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Setup Buttons, and their Listeners
JButton RefreshButton = new JButton("Refresh");
RefreshButton.addActionListener(new RefreshListener());
// GUI stuff, working and unrelated, I think //
// Setup Labels
MainLabel = new JLabel("Observations");
Font Header = new Font("serif", Font.BOLD, 32);
MainLabel.setFont(Header);
FooterLabel = new JLabel("FooterText", SwingConstants.CENTER);
Font Footer = new Font("serif", Font.PLAIN, 12);
FooterLabel.setFont(Footer);
FooterLabel.setForeground(Color.DARK_GRAY);
// Setup Panel that holds the buttons
MyDrawPanel buttonPanel = new MyDrawPanel();
Dimension d = new Dimension(150,100);
buttonPanel.setPreferredSize(d);
buttonPanel.setBackground(Color.darkGray);
//drawPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); // Old Layout, Grid works better.
buttonPanel.setLayout(new GridLayout(9, 1, 0, 1));
// Setup Text area for displaying Observations
ListText = new JTextArea(24,48);
ListText.setLineWrap(true);
ListText.setMargin(new Insets(2,4,0,0)); // Text Padding, Top/Left
JScrollPane scroller = new JScrollPane(ListText);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
// Default Values
ListText.setText("Nothing currently loaded...\n");
ListText.append("Please import a HTML file.\n");
ListText.setEditable(false);
// Put Everything into JFrames
MainFrame.getContentPane().add(BorderLayout.NORTH, MainLabel);
MainFrame.getContentPane().add(BorderLayout.EAST, buttonPanel);
MainFrame.getContentPane().add(BorderLayout.CENTER, scroller);
MainFrame.getContentPane().add(BorderLayout.SOUTH, FooterLabel);
buttonPanel.add(ImportButton);
buttonPanel.add(RefreshButton);
buttonPanel.add(ExitButton);
//MainFrame Settings
MainFrame.setSize(720, 480);
MainFrame.setVisible(true);
MainFrame.setResizable(false);
} // Close Go
public void testing() {
System.out.print("TestMethodSuccess");
}
class ImportListener implements ActionListener { // Inner Class
public void actionPerformed(ActionEvent event) {
ListText.append("Import Button!\n");
}
}
class RefreshListener implements ActionListener { // Inner Class
public void actionPerformed(ActionEvent event) {
ListText.append("Refresh Button!\n");
gui.testing(); // Error traces to here //
}
}
class CloseListener implements ActionListener { // Inner Class
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
} // Close Class
我还尝试了以下主方法,而不是使用begin()方法,在同一个地方发布。我认为这与变量阴影有关吗?无论如何,解决方案或解释任何一个都会很棒。
public static void main (String[] args) {
MainGUI gui = new MainGUI();
gui.go();
}