gpt4 book ai didi

java - 我正在尝试使用 GUI 使用 Java 创建 BMI 计算器,但收到语法错误

转载 作者:行者123 更新时间:2023-12-01 18:34:10 25 4
gpt4 key购买 nike

我正在尝试编写一个 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)

最佳答案

你是shadowing变量resultat。替换

JLabel resultat = new JLabel("");

resultat = new JLabel();

关于java - 我正在尝试使用 GUI 使用 Java 创建 BMI 计算器,但收到语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22770769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com