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 2.2.

Figure 2.2. Spotting errors in the Source Editor.

Image

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 Saluton program:

cannot find symbol.
symbol  : variable greting
location: class Saluton


Tip

This book’s official website www.java24hours.com includes source files for all programs you create. If you can’t find any typos or other reasons for errors in the Saluton program but there are still errors, go to the book’s website and download Saluton.java from the. Hour 2 page. Try to run that file instead.


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 in NetBeans to see what happens.)

If you get error messages when creating the Saluton program, double-check that your program matches Listing 2.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 need to be fixed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.209.201