线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException

时间:2014-08-19 10:50:18

标签: 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 );

   } 

} 

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

似乎

 double hourlyWage = Double.parseDouble( wageTextField.getText() );

 double hoursWorked = Double.parseDouble( hourTextField.getText() );
单击按钮时,

wageTextFieldhourTextField为空字符串。检查一下。

<强>解

当执行单击计算按钮(actionPerformed方法)时,您应该检查wageTextFieldhourTextField是否为空。如果其中一个为空,则不应执行上面的两行。