Java Reference
In-Depth Information
Tutorial: Solving Program Errors
Problem “A” is an example of a logic problem. Logic errors are the most difficult to solve because the reason and
location of the error is totally unknown. For instance, if the application generated an incorrect tax amount, you would
probably examine the calculation statement(s). If the calculation looked good, you would then try to verify that the
values entered by the user were actually being used in the calculation. Ninety-nine percent of the time, this would
result in finding the mistake. However, with the error message not appearing, there is no obvious starting point or
procedure to find the error. For instance, is the setText statement wrong, or is the text erased somewhere in the code,
or is the label never defined, or…, or…, or…? Even if you stepped through the source code statements in the Debug
perspective, the error would not be obvious.
The cause of this problem is that the result label is added to the frame in the actionPerformed method after it is
determined that a button other than the display button has been clicked. In other words, when the incorrect values
are entered the first time, the add statement is never reached. This is an example of poor program structure. The
initialize method (that RAD created) is the correct place to specify all of the frame's components. The result label
should have been added to the frame in initialize not in the actionPerformed method.
1.
Cut the following statement from the actionPerformed method and paste it at the end of
the initialize method.
this .add(resultLbl);
2.
Save the source and run the application with the test data again.
Well, problem “A” is solved (because the error message is now being displayed). However, a new problem (which
we will call problem “E”) has been uncovered: when the frame is initially displayed, the result label has the text Label.
Dang, another error!! (Changes can often have unintended results and this is why you should test, test, and then test
again.) Can you figure out how to fix this?
This is an excellent example of a “soft” error. “Hard” errors are program failures, incorrect results, etc. Problems
“A” and “D” are examples of hard errors. This new error is considered a soft error because it does not produce
incorrect results; however, soft errors may confuse a user. As mentioned earlier, red text traditionally indicates an
error. So, if correct results are displayed in red (as in problem "B1"), the user may think that the results are incorrect.
Problems “B” and “C” are considered soft errors because the results are correct but poorly formatted.
Soft errors are usually fixed after the hard errors are resolved. However, we will use a new visual component to
solve problem “D.” Since the new component will require an explanation, we will instead solve the soft problems
("B1", “C” and “E”) now, and then solve problem “D.” Problem “B2” is also a soft problem, but formatting is a topic
that needs an explanation, so we will also tackle it later in the chapter.
In the actionPerformed method, after the double variables are defined, add the following
statement:
3.
resultLbl.setForeground(Color. black );
This will set the result label text to black each time the actionPerformed method is executed. If the label color was
previously changed to red, this will reset the color to black.
4.
In the initialize method, change the label's text to blanks, length to 350, and horizontal
location to 25 (see the following):
resultLbl.setText("");
resultLbl.setSize( new Dimension( 350 , 23));
resultLbl.setLocation( new Point( 25 , 332));
5.
Run the tests again to prove that problems “B1”, “C”, and “E” have been resolved.
 
Search WWH ::




Custom Search