作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个体重指数计算器来练习创建 GUI。但是,我无法弄清楚为什么会出现如下所示的错误。我认为我试图错误地显示 BMI 值。有人可以帮忙吗?
Exception in thread "main" java.lang.NullPointerException at Source.(Source.java:21) at Main.main(Main.java:5)
import javax.swing.JFrame;
public class Main {
public static void main (String args []) {
Source sourceObject = new Source();
sourceObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sourceObject.setSize(275,180);
sourceObject.setVisible(true);
}
}
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Source extends JFrame {
private JLabel item1;
private JLabel item2;
private JLabel item3;
private String weight, height;
private int BMI;
public Source () {
super("Title");
setLayout(new FlowLayout());
item1 = new JLabel("Text");
item1.setToolTipText("This appears on hover");
weight = JOptionPane.showInputDialog(null, "Weight: ");
height = JOptionPane.showInputDialog(null, "Height: ");
item3.setText(String.valueOf(BMI));
add(item1);
add(item2);
add(item3);
}
int BMICalc() {
int weig = Integer.parseInt(weight);
int heig = Integer.parseInt(height);
int BMI = (weig)/(heig * heig);
return BMI;
}
}
最佳答案
实际上 item2
和 item3
已声明但从未实例化。
但实际问题是您正在调用 item3
上的方法。
实例化 Source
构造函数中具有已知值的所有字段。
关于java - 体重指数计算器 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784849/
我希望该按钮(子项)占水平 LinearLayout(其父项)全宽的 25%。代码: LinearLayout ll = new LinearLayout(this); ll.setLayoutPar
我想问一下,当循环开始并再次循环时,字符串变量名称会增加1,我该怎么做。这个程序应该问你要写多少个病人。如果你为前任写信。 10,那么循环将进行10次,它会询问我想要的所有信息,然后将它们添加到我已经
我是一名优秀的程序员,十分优秀!