Исключение в потоке "AWT-EventQueue-0" java.lang.NumberFormatException
Я строю объектную программу в 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 ответа
Вы всегда должны проверять пользовательский ввод перед его использованием. Или перехватите NumberFormatException и переведите его в удобную для пользователя ошибку проверки в пользовательском интерфейсе.
Кажется, что
double hourlyWage = Double.parseDouble( wageTextField.getText() );
double hoursWorked = Double.parseDouble( hourTextField.getText() );
wageTextField
или же hourTextField
пустая строка при нажатии кнопки. Проверь это.
РЕШЕНИЕ
При нажатии кнопки "Рассчитать" (метод actionPerformed) следует проверить, wageTextField
а также hourTextField
не пустые. Если одна из них пуста, две строки выше не должны выполняться.