所以我试图制作一个简单的“区域查找器”程序。它基于文本工作,但是当我添加一个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)
提前致谢!
答案 0 :(得分: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);
您声明并添加到JPanel的这些tlength
和twidth
只是该构造函数范围内的局部变量。而actionPerformed
方法则引用类的实例变量tlength
和twidth
,它们仍为null,因为它们从未实例化过。因此,当您尝试调用tlength.getText()
时,将抛出NullPointerException。
要解决此问题,请将构造函数中的行JTextField tlength = new JTextField();
更改为tlength = new JTextField();
。对JTextField twidth = ...
行执行相同的操作。
答案 1 :(得分:1)
您收到NullPointerException
因为tlength.getText()
为tlength
时您正在呼叫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();
Roddy对这些特定变化的相关性给出了很好的解释。