异常的线程“AWT-EventQueue-0”?

时间:2022-01-26 17:35:54

I'm working on a simple calculator program using java and javax.swing

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

Basically, when you press a button, the program is supposed to fetch the function of that button (number or operation) and display it in the textArea. The whole logic for the calculator itself doesn't matter. Also, there's a clear menu item that clears all the text in textArea.

基本上,当您按下一个按钮时,程序应该获取该按钮(数字或操作)的功能,并在textArea中显示它。计算器本身的逻辑并不重要。此外,还有一个清晰的菜单项,可以清除textArea中的所有文本。

However, every time a press a button, I get the following error:

然而,每当按下一个按钮,我就会得到以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at calculator.CalculatorGUI.actionPerformed(CalculatorGUI.java:106)

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

I'm also getting a similar error when I press the clear menu item, it seems like java doesn't like it when I want to change the text in the text area.

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

Here's my code: (Line 106 is in the actionPerfomed method, I've labeld it for your convenience)

这是我的代码:(第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);
    }
}

Any help would be greatly appreciated! Thanks!

非常感谢您的帮助!谢谢!

2 个解决方案

#1


10  

You don't ever initialize the JTextArea member field called textArea. You are shadowing the member field in your declaration. Try this:

您不会初始化JTextArea成员字段,即textArea。您在声明中隐藏了成员字段。试试这个:

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

instead of

而不是

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

#2


2  

You are not creating the textarea object, JTextArea textArea = new JTextArea(); just defines a local variable in the showCalculator() that hides the class attribute that remains uninitialized, so use textArea = new JTextArea();.

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

#1


10  

You don't ever initialize the JTextArea member field called textArea. You are shadowing the member field in your declaration. Try this:

您不会初始化JTextArea成员字段,即textArea。您在声明中隐藏了成员字段。试试这个:

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

instead of

而不是

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

#2


2  

You are not creating the textarea object, JTextArea textArea = new JTextArea(); just defines a local variable in the showCalculator() that hides the class attribute that remains uninitialized, so use textArea = new JTextArea();.

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