Compile time bugs
- Look at the error message, not just the word error.
- Look for the line number and the comment.
- Examine the offending line character by character.
- If you still can't find it, look at a line earlier in the
program, such as a declaration problem.
- If you still can't see it, ask someone to look over your
shoulder while you explain it to him.
- Strangely, even if they don't listen to you, you will frequently
find the error at this point.
Run time bugs
Typical problems:
- A class constructor sets an object's fields correctly but when I
call a method an error says that some are null.
- Are you defining the field identifier local to the constructor?
Don't redefine fields within the constructor just assign values
to them.
- Program works interactively but doesn't work from file input, I give up!
- look at data file for data file format error
- make sure the data file ends in a newline (insidious)
- Program doesn't return from subroutine.
- Look for infinite "for" or "while" loops.
- Output numbers are shown as NaN
- Divide zero by zero.
- Output numbers are shown as infinity
- Divide non-zero by zero.
General Comments
Try keeping a file listing solved bugs, especially the ones that took
the longest to find. You will find that the most aggravating always
turn out to be "stupid" errors. This can be an excellent check list
for future debugging.
Errors of intension - logical errors
Once your program is running and seems to be correct don't assume it
is. The most common error of all is assuming that the code is correct
and not testing it. FINDING A SMALL SET OF TESTS and testing your
program is essential. Next, really LOOK AT THE OUTPUT and convince
yourself that the program is correct. Mark the output with comments
or by hand calculations to convince your tutor that your program does
what you say it does. Don't be disappointed by a discerning tutor.
Avoidable Errors in Assignments
- Divide by zero occurs on some input. You will lose marks here.
- Zero or Negative integer input is used to declare the size of an array.
You will lose marks here.
- Input format checking - important unless you make explicit assumptions.
- Type checking - important for later assignments.
- In all other cases you must fulfill the requirements of the assignment.
Any assumptions that you make must be made explicitly and included in
internal documentation.
Avoid Side effects
Side effects occur when a method alters the value of an instance
variable but returns another value. Public methods calling private
methods that have side effects can be difficult to follow as in this
example.
class OtherClass {
private int number;
OtherClass (int n) {
number = n;
}
public int number () {
return number;
}
}
public class Bad {
private OtherClass[] list;
private int index = -1;
Bad (int size) {
list = new OtherClass[size];
}
private OtherClass privateMethod (int number) {
for (int i=0;i < list.length; i++) {
if (list[i].number() == number) {
index = i; // < side effect sets index
return list[i];
}
}
index = -1; // < side effect sets index
return null;
}
public int publicMethod (int number) {
OtherClass element = privateMethod(number); // < privateMethod returns "element"
return index; // < returns "index" ignores "element"
}
}