簡體   English   中英

線程“ AWT-EventQueue-0”中的異常java.lang.NumberFormatException:空

[英]Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: empty

有人可以幫助我嗎? 我在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)

這是我的代碼的一部分,可能會導致異常:

   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); 
        }
}   

誰能告訴我如何解決? 謝謝

您必須檢查costmeal.getText()和qtypizza.getText()均包含有效數字,根據快照代碼和堆棧跟蹤,似乎其中之一為空。

NumberFormatException是當您嘗試將String轉換為數字(int,double,float等)時可能引發的異常。 您必須通過將代碼放在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); 
    }
}   

要么

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM