【问题标题】:Exception in thread "AWT-EventQueue-0"?线程“AWT-EventQueue-0”中的异常?
【发布时间】:2011-03-15 03:41:43
【问题描述】:

我正在使用 java 和 javax.swing 开发一个简单的计算器程序

基本上,当您按下一个按钮时,程序应该获取该按钮的功能(数字或操作)并将其显示在 textArea 中。

计算器本身的整个逻辑并不重要。此外,还有一个 clear 菜单项可以清除 textArea 中的所有文本。

但是,每次按下按钮时,我都会收到以下错误:

线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException 在calculator.CalculatorGUI.actionPerformed(CalculatorGUI.java:106)`

当我按下 clear 菜单项时,我也遇到了类似的错误,当我想更改文本区域中的文本时,似乎 java 不喜欢它。

这是我的代码:(第 106 行在 actionPerfomed 方法中,为方便起见,我已将其标记)

public class CalculatorGUI implements ActionListener 
{

    // The calculator for actually calculating!
    // private RPNCalculator calculator;

    // The main frame for the GUI
    private JFrame frame;

    // The menubar for the GUI
    private JMenuBar menuBar;

    // The textbox
    private JTextArea textArea;
    private JScrollPane scrollArea;

    // Areas for numbers and operators
    private JPanel numKeysPane;
    private JPanel opKeysPane;

    private String input;
    final String[] numbers = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
    final String[] operations = { "+", "-", "*", "/" };

    /**
     * Constructor
     */
    public CalculatorGUI() {
        // Initialize the calculator
        calculator = new RPNCalculator();
    }

    /**
     * Initialize and display the calculator
     */
    public void showCalculator() {

        String buttonValue;
        JButton button;
        JMenu menu;
        JMenuItem menuItem;

        // Create the main GUI components
        frame = new JFrame("RPN Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        numKeysPane = new JPanel(new GridLayout(4, 3));
        opKeysPane = new JPanel(new GridLayout(2, 2));

        initializeMenu();
        initializeNumberPad();
        initializeOps();


        JTextArea textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane scrollArea = new JScrollPane(textArea);

        // Create the components to go into the frame and
        // stick them in a container named contents
        frame.getContentPane().add(numKeysPane, BorderLayout.CENTER);
        frame.getContentPane().add(scrollArea, BorderLayout.NORTH);
        frame.getContentPane().add(opKeysPane, BorderLayout.LINE_END);

        // Finish setting up the frame, and show it.
        frame.pack();
        frame.setVisible(true);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e) {
        String s = (String)e.getActionCommand();
        // calculator.performCommand(s);
        textArea.append(s + " ");      // <<--- THIS IS LINE 106
    } 

    /**
     * Initialize the number pad for the calculator
     */


    private void initializeNumberPad() {
        JButton button;
        for (int i = 0; i < numbers.length; i++) {
            button = new JButton(numbers[i]);
            button.addActionListener(this);
            numKeysPane.add(button);
        }
    }

    private void initializeOps(){
        JButton button;
        for (int i = 0; i < operations.length; i++){
            button = new JButton(operations[i]);
            button.addActionListener(this);
            opKeysPane.add(button);
        }
    }

    /**
     * Initialize the menu for the GUI
     */
    private void initializeMenu() {
        JMenu menu;
        JMenuItem menuItem;
        JMenuItem menuItem2;

        // Create a menu with one item, Quit
        menu = new JMenu("Calculator");
        menuItem = new JMenuItem("Quit");
        // When quit is selected, destroy the application
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // A trace message so you can see this
                // is invoked.
                System.err.println("Close window");
                frame.setVisible(false);
                frame.dispose();
            }
        });
        menuItem2 = new JMenuItem("Clear");
        menuItem2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                textArea.setText("");
            }
        });
        menu.add(menuItem2);
        menu.add(menuItem);

        // Create the menu bar
        menuBar = new JMenuBar();
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);
    }

    /**
     * Helper method for displaying an error as a pop-up
     * @param message The message to display 
     */
    private static void errorPopup(String message) {
        JOptionPane.showMessageDialog(null, message);
    }
}

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 建议您提供 sscce(请参阅 sscce.org)。以上不编译...缺少RPNCalculator
  • 我刚刚编辑了代码,所以它不需要 RPNCalculator 类。对于那个很抱歉。 =)

标签: java swing


【解决方案1】:

您永远不会初始化名为textArea 的JTextArea 成员字段。您正在隐藏声明中的成员字段。试试这个:

 textArea = new JTextArea();
 textArea.setEditable(false);
 textArea.setLineWrap(true);
 textArea.setWrapStyleWord(true);

而不是

 JTextArea textArea = new JTextArea();
 textArea.setEditable(false);
 textArea.setLineWrap(true);
 textArea.setWrapStyleWord(true);

【讨论】:

  • Eclipse 可以警告它,如果你打开警告。在 Java Compiler/Errors/Warnings/Name Shadowing and Conflicts 下。
  • 或者使用this.textArea避免再次犯错?
  • 这是初始化异常吗?
【解决方案2】:

您没有创建 textarea 对象,JTextArea textArea = new JTextArea(); 只是在 showCalculator() 中定义了一个局部变量,该变量隐藏了未初始化的类属性,因此请使用 textArea = new JTextArea();。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2013-04-28
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多