PDA

View Full Version : Please Help me out- please check the error


java.java
07-28-2008, 11:42 AM
Hi I have designed a payroll program- for my college project but i am unable to run the program, Please help me checking the error, if you find any error please redesign it please i will be very thankful to you

1. import javax.swing.*;
2. import java.awt.*;
3. import java.awt.event.*;
4.
5. public class payroll extends JFrame
6. {
7.
8.
9. private JLabel daysL,rateL,gpayL,deductionsL,taxL,sssL,cashadvL,totaldL,netpayL;
10. private JTextField daysTF,rateTF,gpayTF,taxTF,sssTF,cashadvTF,deductionTF,totaldTF,netpayTF;
11. private JButton calculateB, exitB;
12.
13. private CalculateButtonHandler cbHandler;
14. private ExitButtonHandler ebHandler;
15.
16.
17. private static final int WIDTH=400;
18. private static final int HEIGHT=500;
19.
20. public payroll()
21. {
22.
23. daysL=new JLabel("Number of days Worked",SwingConstants.RIGHT);
24. rateL=new JLabel("Rate Per Day",SwingConstants.RIGHT);
25. gpayL=new JLabel("Gross Pay",SwingConstants.RIGHT);
26. taxL=new JLabel("With Tax(10%)",SwingConstants.RIGHT);
27. sssL=new JLabel("SSS (8%)",SwingConstants.RIGHT);
28. cashadvL=new JLabel("Cash Advance:",SwingConstants.RIGHT);
29. totaldL=new JLabel("Total Deduction:",SwingConstants.RIGHT);
30. netpayL=new JLabel("Total Net Pay",SwingConstants.RIGHT);
31.
32. daysTF=new JTextField(10);
33. rateTF=new JTextField(10);
34. gpayTF=new JTextField(10);
35. taxTF=new JTextField(10);
36. sssTF=new JTextField(10);
37. cashadvTF=new JTextField(10);
38. totaldTF=new JTextField(10);
39. netpayTF=new JTextField(10);
40. calculateB=new JButton("Calculate");
41. cbHandler=new CalculateButtonHandler();
42.
43. calculateB.addActionListener(cbHandler);
44. exitB=new JButton("Exit");
45. ebHandler=new ExitButtonHandler();
46. exitB.addActionListener(ebHandler);
47.
48.
49.
50. setTitle("7'11 Payroll");
51.
52.
53. Container pane=getContentPane();
54.
55.
56. pane.setLayout(new GridLayout(9,2));
57.
58.
59. pane.add(daysL);
60. pane.add(daysTF);
61. pane.add(rateL);
62. pane.add(rateTF);
63. pane.add(gpayL);
64. pane.add(gpayTF);
65. pane.add(taxL);
66. pane.add(taxTF);
67. pane.add(sssL);
68. pane.add(sssTF);
69. pane.add(cashadvL);
70. pane.add(cashadvTF);
71. pane.add(totaldL);
72. pane.add(totaldTF);
73. pane.add(netpayL);
74. pane.add(netpayTF);
75. pane.add(calculateB);
76. pane.add(exitB);
77.
78. setSize(WIDTH, HEIGHT);
79. setVisible(true);
80. setDefaultCloseOperation(EXIT_ON_CLOSE);
81.
82. }
83. public class CalculateButtonHandler implements ActionListener
84. {
85. public void actionPerformed(ActionEvent e)
86. {
87.
88. double days,rate,gpay,tax,sss,totald,cashadv,netpay;
89. days=Double.parseDouble(daysTF.getText());
90. rate=Double.parseDouble(rateTF.getText());
91. gpay=Double.parseDouble(gpayTF.getText());
92. tax=Double.parseDouble(taxTF.getText());
93. sss=Double.parseDouble(sssTF.getText());
94. totald=Double.parseDouble(totaldTF.getText());
95. netpay=Double.parseDouble(netpayTF.getText());
96. cashadv=Double.parseDouble(cashadvTF.getText());
97.
98. gpay=(days*rate);
99. tax=(gpay*.10);
100. sss=(tax*.08);
101. totald=(tax+sss)+cashadv;
102. netpay=(gpay-totald);
103.
104.
105.
106. }
107. }
108.
109. private class ExitButtonHandler implements ActionListener
110. {
111. public void actionPerformed(ActionEvent e)
112. {
113. System.exit(0);
114. }
115. }
116.
117. public static void main (String [] args)
118. {
119. payroll recObject= new payroll();
120. }
121. }

Aradon
07-28-2008, 11:57 AM
Can you tell us what the error is or what the problem is with the program? If it is a compiler error, please paste it here, otherwise describe the problem.

ess
07-28-2008, 01:16 PM
Like Aradon said, it is quite difficult to help you without knowing the generated error(s).

Having said that though, I have noticed that you are declaring public classes (line 83, 109) in the same file....and have failed to declared methods to handle actions for the exist button on line 46 and the calculate button on line 43

Please compile your application again...and submit any generated errors so that we can help you out.

Cheers
~E

java.java
07-28-2008, 01:42 PM
thanks Aradon and ess

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String

How can i solve this

java.java
07-28-2008, 01:45 PM
you know what happens when i try to run this program with an empty box it shows me this error
but i want the result to be calculated by the program

Please help me and make it possible

ess
07-28-2008, 04:24 PM
thanks Aradon and ess

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String

How can i solve this

The generated exception is a run time exception of type NumberFormatException which indicates that either 1) the parsing failed because you've submitted the wrong data type, or 2) you failed to submit any data at all.

Although it is not apparent to many java programmers, but you should ALWAYS use a try and catch block(s) when parsing data from one type to another in order to avoid any run time exceptions.

I will only demonstrate line 89, but should do the rest where you are parsing into from one type to another.

try {
days=Double.parseDouble(daysTF.getText());
} catch(NumberFormatException e) {
// first...you should always log any errors in a log file...but this is up to you really
// I will use JOptionPane as a demonstration for this error.
JOptionPane.showMessageDialog(null, "Unable to convert to Double, please enter a number for the number of days\n" + e.getMessage());
}

It is a very simple try and catch block...which ensures that the data entered for the number of days can be converted into double

Of course, you shouldn't really use JOptionPane for every single field....as it can get very annoying to keep pressing ok, or cancel for every data entry. Personally, I use red borders around any JTextField with messages next to the field to indicate what went wrong. I will leave this for your imagination and creativity ;)

Hope this helps

Cheers
~E