java - 线程 "AWT-EventQueue-0"java.lang.NumberFormatException 中出现异常

标签 java string exception jcreator

我正在用java构建一个基于对象的程序。应该计算工资并扣除一些税。将使用 JCheckBox 检查税款。问题是当我单击复选框时,它显示 ff 错误:

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)
at WageCalcDriver.itemStateChanged(WageCalcDriver.java:140)
at javax.swing.AbstractButton.fireItemStateChanged(AbstractButton.java:2050)
at javax.swing.AbstractButton$Handler.itemStateChanged(AbstractButton.java:2353)
at javax.swing.DefaultButtonModel.fireItemStateChanged(DefaultButtonModel.java:455)
at javax.swing.JToggleButton$ToggleButtonModel.setSelected(JToggleButton.java:272)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:289)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

这是程序:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;

    public class WageCalcDriver extends JFrame implements ActionListener, ItemListener
{
   private String workertype[] = {"Clerk", "Welder", "Purchaser", "Sales Manager"};
   private JComboBox combo = new JComboBox(workertype);
   private JLabel comboLabel = new JLabel("Type of Work: ");

   private JLabel wageLabel = new JLabel();
   private JTextField wageTextField = new JTextField();

   private JLabel hourLabel = new JLabel();
   private JTextField hourTextField = new JTextField();

   private JLabel gwageLabel = new JLabel();
   private JTextField gwageTextField = new JTextField();

   private JLabel deductions = new JLabel("Deductions: ");
   private JCheckBox sss = new JCheckBox("SSS");
   private JCheckBox philhealth = new JCheckBox("PHILHEALTH");
   private JCheckBox pagibig = new JCheckBox("PAG-IBIG");

   private JLabel netLabel = new JLabel("Net Income: ");
       private JTextField netTextField = new JTextField("");

        JLabel detailsLabel = new JLabel ("Deduction Info:");
        String names[] = {"PAG-IBIG", "SSS", "PHILHEALTH"};
        JComboBox detailCombo = new JComboBox(names);
        JTextField detail = new JTextField(5);

       private JButton calculateJButton = new JButton("CALCULATE");

   public WageCalcDriver()
   {
      Container c = getContentPane();
      c.setLayout( null ); 

      c.add(comboLabel);
      c.add(combo); 
      c.add( wageLabel );
      c.add( wageTextField );
      c.add( hourLabel );
      c.add( hourTextField );
      c.add( gwageLabel );
      c.add( gwageTextField );
      c.add( deductions );
      c.add( sss );
      c.add( philhealth );
      c.add( pagibig );
      c.add( netLabel );
      c.add( netTextField );
      c.add( calculateJButton );
      c.add( detailsLabel );
      c.add( detailCombo );
      c.add( detail );

      comboLabel.setBounds(16,20,100,21);       combo.setBounds(120,20,100,20);
      wageLabel.setBounds( 16, 50, 100, 21 );   wageTextField.setBounds( 120, 50, 100, 21 );
      hourLabel.setBounds( 16, 80, 100, 21 );   hourTextField.setBounds( 120, 80, 100, 21 );
      gwageLabel.setBounds( 16, 110, 100, 21 ); gwageTextField.setBounds( 120, 110, 100, 21 );

      deductions.setBounds(16,140,100,21);      sss.setBounds(120,140,100,21);
                                                philhealth.setBounds(120,170,100,21);
                                                pagibig.setBounds(120,200,100,21);

      netLabel.setBounds(16,230,100,21);        netTextField.setBounds(120,230,100,21);
      detailsLabel.setBounds(16,260,100,21);    detailCombo.setBounds(120,260,100,21);
      detail.setBounds(16,290,200,100);

      calculateJButton.setBounds( 50, 600, 120, 24 );

      gwageLabel.setText( "Gross wages:" );
      wageLabel.setText( "Hourly wage:" );
      hourLabel.setText( "Hours worked:" );


      wageTextField.setHorizontalAlignment(JTextField.RIGHT );
      hourTextField.setHorizontalAlignment(JTextField.RIGHT );
      gwageTextField.setHorizontalAlignment(JTextField.RIGHT );

      gwageTextField.setEditable( false );
      netTextField.setEditable( false );

      combo.addItemListener(this);
      detailCombo.addItemListener(this);
      calculateJButton.addActionListener(this);        
      sss.addItemListener(this);
      philhealth.addItemListener(this);
      pagibig.addItemListener(this);

      setTitle( "Wage Calculator" ); 
      setSize( 245, 700 );           
      setVisible( true );            

    //      netTextField.setText(netinc()); 

       } //constructor end

       public void actionPerformed(ActionEvent e)
   {
        if(e.getSource() == calculateJButton)
    {
      double hourlyWage = Double.parseDouble( wageTextField.getText() );

      double hoursWorked = Double.parseDouble( hourTextField.getText() );

      WageCalcObj object = new WageCalcObj(hoursWorked, hourlyWage);

      gwageTextField.setText(object.calculatePay());
    }       
   }

   public void itemStateChanged(ItemEvent e)
   {
    if(e.getSource() == sss)
    {
        if (e.getStateChange() == ItemEvent.SELECTED)
        {
     double hourlyWage = Double.parseDouble( wageTextField.getText() );
     double hoursWorked = Double.parseDouble( hourTextField.getText() );
     WageCalcObj object_sss = new WageCalcObj(hoursWorked, hourlyWage);
     netTextField.setText(object_sss.deduct_sss());
        }       
    }

    if(e.getSource() == pagibig)
    {
     double hourlyWage = Double.parseDouble( wageTextField.getText() );
     double hoursWorked = Double.parseDouble( hourTextField.getText() );
     WageCalcObj object_pagibig = new WageCalcObj(hoursWorked, hourlyWage);
     netTextField.setText(object_pagibig.deduct_pagibig()); 
    }

    if(e.getSource() == philhealth)
    {
     double hourlyWage = Double.parseDouble( wageTextField.getText() );
     double hoursWorked = Double.parseDouble( hourTextField.getText() );
     WageCalcObj object_philhealth = new WageCalcObj(hoursWorked, hourlyWage);
     netTextField.setText(object_philhealth.deduct_philhealth());   
    }



   }

   public static void main( String[] args )
   {
      WageCalcDriver app = new WageCalcDriver();
      app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } 

} 

我应该怎么做才能纠正这个问题?预先感谢:)

最佳答案

在使用用户输入之前,您始终必须验证它。或者捕获 NumberFormatException 并将其转换为用户界面上的用户友好验证错误。

关于java - 线程 "AWT-EventQueue-0"java.lang.NumberFormatException 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25381511/

相关文章:

java - 如何使用 Gracenote ApI 在 android 中获取歌曲封面艺术?

java - 如何禁用Java,Android中的所有按钮

c++ - 如何在循环移位后从给定字符串中找到一组不同的字符串?

c++ - 在 C++ 中复制和反转 char*

android - 如何捕获没有互联网异常错误?

java - 包含 buildscript 存储库定义失败

java - 为什么我无法返回selenium方法中的值?

python - 有没有办法让 python str.partition 忽略大小写?

python - 当函数返回 None 或抛出异常时使用默认值的 Pythonic 方式是什么?

exception - 是什么导致了我的 OLEDbException,IErrorInfo.GetDescription 因 E_FAIL(0x80004005) 而失败