Java Reference
In-Depth Information
Syntax Errors
A syntax error is an error caused by code statements that violate one or
more syntax rules of the Java programming language. Typing mistakes often
cause syntax errors. When you compile Java source code, TextPad lists syntax
errors in a Command Results window , as shown in Figure 2-29.
Command Results
listed in Selector
window
syntax errors
are displayed
Command
Results window
pointers
FIGURE 2-29
When it encounters syntax errors, the compiler attempts to isolate the syntax
error by displaying a line of code with pointers pointing to the first incorrect
character in that line. The error, however, may not be at that exact point. For
instance, if you omit a necessary semicolon at the end of a line, TextPad may dis-
play error messages, similar to those shown in Figure 2-29. Both errors are due
to the same missing semicolon. Without the semicolon in the first line, the com-
piler cannot find the end of the first line and thus generates an error message
with the file name, the line number, and the error message, Invalid type expres-
sion. The compiler then tries to compile the two lines together, which results in
a second error message, Invalid declaration. Once the semicolon in the code is
corrected, neither error message will display.
A rule of thumb to follow when debugging syntax errors is to correct the
first mistake in a long list. Doing so may reduce the total number of errors
dramatically and allow you to focus on the cause of the remaining errors.
The most common mistakes that cause syntax errors are capitalization,
spelling, the use of incorrect special characters, and omission of correct punc-
tuation. Table 2-6 on the next page lists some common syntax errors, the error
messages that indicate the specific syntax error, and the method of correction.
 
Search WWH ::




Custom Search