Compile time bugs


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


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"
  }
}