0

所以我尝试制作一个简单的“区域查找器”程序。它基于文本工作,但是当我添加 GUI 时它会出错,我不知道是什么原因造成的。对不起,如果我的拼写错误,我不是最伟大的拼写者。

代码

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class AF implements ActionListener {

    double length;
    double width;
    double answer;

    JTextField twidth = null;
    JTextField tlength = null;

    void AFWindow() {

        JFrame AFwindow = new JFrame();
        JPanel pan2 = new JPanel(new GridBagLayout());
        AFwindow.setVisible(true);
        AFwindow.setSize(250, 150);
        AFwindow.setResizable(false);
        GridBagConstraints c = new GridBagConstraints();
        AFwindow.add(pan2);
        pan2.setBackground(Color.LIGHT_GRAY);

        c.gridx = 0;
        c.gridy = 5;
        JTextField tlength = new JTextField();
        tlength.setText("    Length    ");
        pan2.add(tlength, c);

        c.gridx = 0;
        c.gridy = 0;
        JTextField twidth = new JTextField();
        twidth.setText("     Width     ");
        pan2.add(twidth, c);

        JButton Find = new JButton("Ok");

        c.gridx = 0;
        c.gridy = -5;
        pan2.add(Find);
        Find.addActionListener(this);
        Find.setActionCommand("ok");
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("ok")) {
            try {
                this.length = Double.parseDouble(tlength.getText());
                this.width = Double.parseDouble(twidth.getText());

            } catch (NumberFormatException ex) {
                System.out.println("There was an issue!");
            }
        }
    }

    int area = (int) (length * width);

    public void answer() {
        JFrame answer = new JFrame();
        answer.setVisible(true);
        answer.setBackground(Color.yellow);
        JPanel pan2 = new JPanel();
        JLabel howanswer = new JLabel("Your answer is" + area
                + "We got this by multiplaying the length and width");
        pan2.add(howanswer);
    }
}

错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at AF.actionPerformed(AF.java:53)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

提前致谢!

4

2 回答 2

2

在你的课堂上,你有:

JTextField tlength = null;
JTextField twidth = null;

然后在构造函数中你有:

JTextField tlength = new JTextField();
tlength.setText("    Length    ");
pan2.add(tlength, c);
...
JTextField twidth = new JTextField();
twidth.setText("     Width     ");
pan2.add(twidth, c);

这些tlength以及twidth您声明并添加到 JPanel 的这些只是该构造函数范围内的局部变量。该actionPerformed方法改为引用的实例变量tlengthand twidth,它们仍然为空,因为它们从未被实例化。所以当你尝试调用时tlength.getText(),你会抛出一个 NullPointerException。

要修复,JTextField tlength = new JTextField();请将构造函数中的行更改为tlength = new JTextField();. 对线做同样的事情JTextField twidth = ...

于 2012-07-05T06:06:23.740 回答
1

你得到 aNullPointerException因为你打电话tlength.getText()when tlengthis null。你会遇到同样的问题twidth

要解决此问题,请在触发操作之前实例化这些变量。

具体来说,进行以下更改。

JTextField tlength = new JTextField();
JTextField twidth = new JTextField();
// Update the lines above to the following.
this.tlength = new JTextField();
this.twidth = new JTextField();

罗迪很好地解释了为什么这些特殊变化是相关的。

于 2012-07-05T06:01:09.710 回答