Debugging Tips
Not Found Error.
If Java cannot find the item you are referring to, it will report an „X not found” error, where X is the class, method, variable, or package being referred to.
Scope Error.
It would be a syntax error to refer to a method's parameters or other local variables from outside the method.
Type Error.
It would be a syntax error to use an argument whose type does not match the type of its corresponding parameter.
When to Use Return.
All method definitions except constructors must declare a return type.
Method Call.
The signature of the method call – its name and the number, types and order of its arguments – must exactly match the signature of the method definition.
Side Effects.
An unintended change to an object is called a side effect. In designing methods, care should be taken to ensure that the method does not produce unwanted side effects in objects passed as reference parameters.
Indentation.
Indentation can improve the readability of a program but does not affect its logic. Braces must be used to group statements in the if clause.
Significant Digits.
In using numeric data, be sure the data type you choose has enough precision to represent the values your program needs.
Integer Division.
A common source of error among beginning programmers is forgetting that integer division always gives an integer result.
Equality and Assignment.
A common semantic error of beginning programmers is to use the assignment operator (=) when the equality operator (==) is intended.
Division.
Using the correct type of literal in division operations is necessary to ensure that you get the correct type of result.
Test, Test, Test!
The fact that your program runs correctly on some data is no guarantee of its correctness. The more testing, and the more careful the testing, the better.
Loop indentation.
Loop indentation has no effect on how Java interprets the loop. The loop body is determined entirely by the syntax of the for statement.
Missing Braces.
A common programming error for novices is to forget to use braces to group the statements they intend to put in the loop body. When braces are not used, only the first statement after the loop heading will be iterated.
Localization.
Encapsulating code in a method removes the need to have the same code at several locations in a program. By localizing the code in this way, you make it easier to modify and debug.
Switch without break.
A common error in coding switch-based multiway selection is forgetting to put a break statement at the end of each clause. This may cause more than one case to be executed.
Zero versus Unit Indexing.
Syntax and semantic errors will result if you forget that strings are zero indexed. In a string of N characters, the first character occurs at index 0 and the last at index N – 1. This is different from the String.length() method, which gives the number of characters in the string, counting from 1.
substring(int p1, int p2).
Don't forget that the second parameter in the substring() methods refers to the character just past the last character in the substring.
Off-by-One Errors.
Loops should be carefully checked to make sure they don't commit an off-by-one error. During program testing, develop data that tests the loop variable's intitial and final values.
Because of zero indexing, the last element in an array is always length – 1. Forgetting this fact can cause an off-by-one error.
The expressions s.charAt(0) and s.substring(1) will generate exceptions if s is the empty string.
Sorting algorithms are highly susceptible to an off-by-one error. To sort an array with N elements, you generally need to make N – 1 passes.
Array Subscripts.
In developing array algorithms, it is important to design test data that show that array subscripts do not cause runtime errors.
Array Length.
A common syntax error involves forgetting that for arrays length is an instance variable, not an instance method, as it is for Strings.
Array Instantiation.
Creating a new array does not also create the objects stored in the array. They must be instantiated separately. It is a semantic error to refer to an uninstantiated (null) array element.
Array Initialization.
Initializer statements should be used only for relatively small arrays.
Array Parameter.
When declaring an array parameter, empty brackets must be used either after the array name or after the type name to distinguish it from a nonarray parameter.
Passing an Array Argument.
It is a syntax error to use empty brackets when passing an array argument to a method, where only the array's name should be used. Empty brackets are only used when declaring an array variable.
Clearing the JPanel.
Swing components, such as JPanel, do not automatically clear their backgrounds. This must be done explicitly in the paintComponent() method.
End of Binary File.
Because a binary file does not have an end-of-file character, it would be an error to use the same loop-entry conditions we used in the loops we designed for reading text files.
Interpreting Binary Data.
The fact that you can read the data in a binary file is no guarantee that you are interpreting it correctly. To interpret it correctly, you must read it the same way it was written.
Bounding the Repetition.
An infinite repetition will result if a recursive definition is not properly bounded.
Infinite Recursion.
An unbounded or incorrectly bounded recursive algorithm will lead to infinite repetition. Care must be taken to get the boundary condition correct.
Return Type.
A common error with nonvoid recursive algorithms is forgetting to make sure that return statements that contain a recursive call yield the correct data type.
Recursive Search.
For the recursive search method to work properly, it must be called with the correct value for the head parameter.
Content Pane.
A JFrame cannot directly contain GUI elements. Instead, they must be added to its content pane, which can be retrieved using the getContentPane() method.
Attempting to add a component directly to a JApplet or a JFrame will cause an exception. For these top-level containers, components must be added to their content panes.
Top-level containers, such as JFrame, are the only ones that use a content pane. For other containers, such as JPanel, components are added directly to the container.
Starvation.
A high priority thread that never gives up the CPU can starve lower-priority threads by preventing them from accessing the CPU.
Thread Control.
Just breaking a program into two separate threads won't necessarily give you the desired performance. It might be necessary to coordinate the threads.
Stopping a Thread.
The best way to stop a thread is to use a boolean control variable whose value can be set to true or false to exit the run() loop.
Wait/Notify.
It's easy to forget that the wait() and notify() methods can only be used within synchronized methods.
Reserved Port Numbers.
Port numbers below 1024 are reserved for system use and should not be used by an application program.
Socket Streams.
After writing to or reading from a socket I/O stream, do not close the stream. That would make the socket unusable for subsequent I/O.
Bytes and Chars.
It is a syntax error to compare a byte and a char. One must be converted to the other using an explicit cast operator.
Null Reference.
A common programming error is the attempt to use a null reference to refer to an object. This usually means the reference has not been successfully initialized.
List Traversal.
A common error is designing list-traversal algorithms is an erroneous loop-entry or loop-exit condition. One way to avoid this error is to hand trace your algorithm to make sure your code is correct.
3
geminus