Fixing Errors
As you compose a program in the NetBeans source editor, errors are flagged with a red alert icon to the left of the editor pane, as shown in Figure 4.3.
FIGURE 4.3 Spotting errors in the source editor.
The icon appears on the line that triggered the error. You can click this icon to display an error message that explains the compiler error with these details:
- The name of the Java program
- The type of error
- The line where the error was found
Here’s an example of an error message you might see when compiling the Splash program:
cannot find symbol. symbol : variable greting location: class Splash
The error is the first line of the message: cannot find symbol. These messages often can be confusing to new programmers. When the error message doesn’t make sense to you, don’t spend much time trying to figure it out. Instead, take a look at the line where the error occurred and look for the most obvious causes.
For instance, can you determine what’s wrong with the following statement?
System.out
.println(greting);
The error is a typo in the variable name, which should be greeting instead of greting. (Add this typo on purpose in NetBeans to see what happens.)
If you get error messages when creating the Splash program, double-check that your program matches Listing 4.2 and correct any differences you find. Make sure that everything is capitalized correctly and all punctuation marks such as {, }, and ; are included.
Often, a close look at the line identified by the error message is enough to reveal the error (or errors) that needs to be fixed.
Take note that the line number displayed with the error message isn’t always the place where an error needs to be fixed. Examine the statements that are directly above the error message to see whether you can spot any typos or other bugs. The error usually is within the same programming block.