Download Basic Error Checking (Java) and more Summaries Java Programming in PDF only on Docsity!
Basic Error Checking
(Java)
By: Kathleen Lockhart
Overview
How to interpret a compiler error message
General Debugging Strategies
10 Common Compiler Errors
What to do when changing code is not affecting error messages
Limits of this workshop
Strategies
Start checking from the first reported error. Often errors affect more than one part of a program and the first instance is generally nearest to the actual error.
Sometimes correcting one error will cause many others to appear. This is especially true with parenthesis or bracket errors. This is usually a sign of forward progress and should not be immediately worried about.
10 Common Errors
‘)’ Expected
Illegal Start of Expression
‘;’ Expected
Reached end of file while parsing
Cannot find symbol
Variable NAME might not have been initialized
Null pointer exception
Array index out of bounds exception
Incompatible types
Method(VariableType1) cannot be applied to (VariableType2)
Illegal Start of Expression
This error is usually shown after the code that caused it. Something was gone wrong that causes an error when trying to move to the next part of the code.
If the error occurs at the start of a method go to the previous method and make sure all of your brackets are matched properly.
If the error occurs at a normal line of code check the line above it for mismatched parentheses or missing or mistyped semi-colons.
‘;’ Expected
Often, a semi-colon is missing at the end of a line of code
If this error is occurring where there should not be a semi- colon (e.g. in the middle of a method declaration) this could be a sign that the previous method does not end properly. Check to make sure that all opening brackets have a matching closing bracket.
Cannot Find Symbol – Part 1
The variable, method, etc. you are trying to use cannot be found by the computer.
First check your spelling, capitalization, and grammar. Methods should be followed by parentheses, variables should not.
Check your classes: If you’re trying to use methodA() from Class1 inside of Class2 you need to either say Class1.methodA() (static methods) or create an instance of Class1 and use that to call the method (non-static methods), e.g.: Class1 c1 = new Class1(); c1.methodA();
Cannot Find Symbol – Part 2
If you’re working with multiple classes make sure you import the classes you need. E.g.: import Class1;Be careful to include any packages you are using (e.g. java.util.Vector, java.text.NumberFormat, folder1.Class2, etc.)
If you are using packages do not compile in EditPlus (Eclipse is fine.) Instead compile in the command prompt, using the command javac – d. ClassName.java
Null Pointer Exception
The variable being accessed has not been initialized yet.
Go to where you declare the variable and look for anywhere you put data into it. If you are using a method to do this search through the method and make sure there are no problems with it.
Array Index Out of Bounds
Exception
The entry of the array being accessed does not exist
This is a common problem with for loops.
Check the conditions of the for loop having the problem. Often they look like this: - for(int i=0;i<array.length;i++) - Because arrays start counting at 0 make sure the limit in your second term is one less than the number of entries you actually want to search. In the example above this means stopping at one entry lower than the array length
Method(VariableType1) cannot be
applied to (VariableType2)
A method is being passed the wrong parameters.
Check to order of the parameters you are passing, they have to match what is coded in your constructor exactly or an error will occur
Especially with ints and doubles make sure the variables you are using are the right type
Changing Code Is Not Affecting
Error Messages
Make sure you re-save and re-compile code every time you make changes. This is almost always the reason why the program is not responding to edits.