Understanding and troubleshooting errors are a big part of being successful Java programmers. As much as we’d like to believe it, we are not perfect coders every time. We need to know some tips and tricks for finding and fixing errors.
Whenever you are experimenting with a new feature or learning new functionality, you should also try to make mistakes. For example, in the HelloPrinter program, what happens if you leave out one of the quotation marks? What if you leave out both? What if you spell println
wrong? These kinds of experiments help you remember what you have practiced. They also help with debugging, because you learn what the error messages mean.
Programming and debugging should go hand in hand. Don’t just write a bunch of code and then perform trial-and-error debugging until it all works. Instead, start with a program that does something and make small modifications, debugging them as you go, until the program does what you want. That way, you will always have a working program, and isolating errors will be easier.
Finally, programming sometimes brings out strong emotions. If you are struggling with a difficult bug, you might feel angry, despondent, or embarrassed. Remember that you are not alone, and virtually every programmer has had similar experiences. Don’t hesitate to reach out to a friend and ask questions! (adapted from Think Java Section 1.9).
In Java, there are three kinds of possible errors we might have to deal with in our program:
- compile-time errors
- run-time errors
- logic errors
Errors in Action
Compile-Time Errors
Compile time errors happen because you violate a Java language rule. This could include missing parenthesis or curly brackets or missing semicolon.
Eclipse will often give you a line number on a compile-time error. They appear to the left of the code.
If you do not see line numbers, you need to enable them.
Windows: Window — Preferences — General — Editors — Text editors and place a checkmark next to “Show line numbers”
Mac: Eclipse — Preferences — General — Editors — Text editors and place a checkmark next to “Show line numbers”
With compile time errors, the line where the error occurs will display in the error message in the Console.
You will also see this icon next to the line number when you try to save the java file. It can also be a strong hint that something’s wrong.
Finally, if you see a red x next to the file name, it can also be a hint that there’s an error. You’ll also see that same icon throughout the whole project in the Project Explorer.
Often with compile time errors, Eclipse will warn you that something is wrong before it tries to run the program.
Fixing Compile Time Errors
There is no one way to fix a compile time error. Depending on the error, the solution will change. Again, review the line number and determine if the Java language rules have been broken on that line. If it’s not apparent, check on the line directly above or directly below. Sometimes the compiler reports the place in the program where the error was detected, not where it actually occurred. Some errors are more descriptive and you can quickly resolve them.
If you cannot find the error, often a quick Google search of the error will result in some other developer’s pleas for help and can lead you to the right fix.
Run-Time Errors
Run-time errors do not appear until after the program has started running. These errors happen when the Java Virtual Machine is executing the byte code and something goes wrong. These errors are also called “exceptions” because they usually indicate that something unexpected has happened.
When a run-time error occurs, the program “crashes” or “terminates” abruptly and displays an error message in the console. The error message will deliver the exception error and attempt to give a line number. This line number is when the error occurred but it might not be where the code necessarily needs to be changed.
Java has a hierarchy of different exceptions that can occur throughout program runs. We will save that fun for later.
Logic Errors
If your program has a logic error, it will compile and run without generating error messages, but it will not do the right thing. Instead, it will do exactly what you told it to do.
Something is wrong with my program. It does not make sense. Did I ask the right question? Did I print the wrong value? The developer has made a logical error in the program. However, the program does run without errors and it does exactly what I tell it to do. Identifying logic errors can be hard because you have to work backward, looking at the output of the program, trying to figure out why it is doing the wrong thing, and how to make it do the right thing. Usually, the compiler and the interpreter can’t help you, since they don’t know what the right thing is.
Up Next: Variables