我正在尝试编制BMI计算器,但出于某种原因我无法让它工作。 我非常喜欢Java,所以请体谅:)。 类和变量的名称都是瑞典语,但并不难理解。 例如langd是长度,vikt是重量。
这是我的代码:
package prog2;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
public class BMI extends JFrame{
JTextField langdfalt;
JTextField viktfalt;
DecimalFormat fmt = new DecimalFormat ("0.00");
JLabel resultat;
BMI(){
super("BMI Kalkylator 1.0");
setLayout(new FlowLayout());
JLabel langd = new JLabel ("Längd (m):");
add(langd);
langdfalt = new JTextField(4);
add(langdfalt);
JLabel vikt = new JLabel ("Vikt (kg):");
add(vikt);
viktfalt = new JTextField(4);
add(viktfalt);
JButton berakna = new JButton ("Beräkna");
berakna.addActionListener(new BeraknaLyss());
add(berakna);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(330, 100);
setVisible(true);
setLocation(820, 400);
JLabel bmiVisning = new JLabel("Ditt BMI-värde är");
add (bmiVisning);
JLabel resultat = new JLabel ("");
add (resultat);
}
class BeraknaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
double vikt = Double.parseDouble(viktfalt.getText());
double langd = Double.parseDouble(langdfalt.getText());
int bmi = (int)beraknaBMI(vikt, langd);
**resultat.setText(String.valueOf(bmi));**
}
}
public double beraknaBMI(double vikt, double langd){
return vikt / (langd * langd);
}
public static void main(String[] args) {
new BMI();
}
}
我得到的错误是第43行的NullPointerException,我加粗了。
以下是所有错误代码:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at prog2.BMI$BeraknaLyss.actionPerformed(BMI.java:43)
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$200(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 :(得分:4)