[英]Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: empty
anyone can help me? 有人可以帮助我吗? I got this exception in Netbeans 我在Netbeans中遇到了这个异常
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:
empty
String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
and this is part of my code that probably makes the exception : 这是我的代码的一部分,可能会导致异常:
private void pizzaMouseClicked(java.awt.event.MouseEvent evt) {
double cMeal = Double.parseDouble(costmeal.getText());
double qPizza = Double.parseDouble(qtypizza.getText());
double cPizaa = 6000;
if(pizza.isSelected())
{
i[0] = (qPizza * cPizaa) + cMeal;
String pMeal = String.format("%.2f", i[0]);
costmeal.setText(pMeal);
}
}
Can anyone tell me how to fix it? 谁能告诉我如何解决? Thank you 谢谢
您必须检查costmeal.getText()和qtypizza.getText()均包含有效数字,根据快照代码和堆栈跟踪,似乎其中之一为空。
NumberFormatException is an Exception that might be thrown when you try to convert a String into a number(int, double, float etc..). NumberFormatException是当您尝试将String转换为数字(int,double,float等)时可能引发的异常。 You have to handle the exception either by putting the code inside try catch or add the exception to the method signature. 您必须通过将代码放在try catch中或将异常添加到方法签名中来处理异常。
private void pizzaMouseClicked(java.awt.event.MouseEvent evt) throws NumberFormatException {
double cMeal = Double.parseDouble(costmeal.getText());
double qPizza = Double.parseDouble(qtypizza.getText());
double cPizaa = 6000;
if(pizza.isSelected())
{
i[0] = (qPizza * cPizaa) + cMeal;
String pMeal = String.format("%.2f", i[0]);
costmeal.setText(pMeal);
}
}
Or 要么
private void pizzaMouseClicked(java.awt.event.MouseEvent evt) {
try{
double cMeal = Double.parseDouble(costmeal.getText());
double qPizza = Double.parseDouble(qtypizza.getText());
double cPizaa = 6000;
if(pizza.isSelected())
{
i[0] = (qPizza * cPizaa) + cMeal;
String pMeal = String.format("%.2f", i[0]);
costmeal.setText(pMeal);
}
}
catch(NumberFormatException e){
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.