Faculty of Informatics at Jordanstown

When your program unexpectedly dies it is time to debug it. You should make a note of what you did to cause the problem as you will have to repeat this a lot to recreate it.

When debugging you are trying to get answers to the following questions

1. What was the problem

In Java when something goes wrong with your program an Exception is thrown, and assuming your code doesn't attempt to catch it it gets reported by the runtime system.

The name of the Exception gives a good indication of what went wrong such as

but others are a bit less clear such as For these you should read the description of the relevent exception class to learn more.

If your code is set up to catch the exception and report an error (for example, the Java Servlet examples all do this) you should when you are debugging also get it to display the exception, eg

    try
    {
        // ...some code in here
    }
    catch(SQLException e)
    {
        out.println("Exception when inserting books into database");
        out.println(e);  // display the message associated with exception
        out.flush(); // ensure the messages get displayed
    }
(Also, each of your catches should display a message that uniquely identifies it so you can tell which one actually caught the Exception)

2. Where did it occur ?

The other important fact for our initial investigation is where exactly in our code is the error happening.

Again in Java if we are not catching the Exception this can be a relatively simple task. The default handler for all uncaught exceptions will produce what is called a stack trace.

        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.(FileInputStream.java:68)
        at java.io.FileReader.(FileReader.java:35)
        at Payroll.readReport(Payroll.java:53)
        at Payroll.main(Payroll.java:26)
A stack trace is a list of which methods had been invoked at the time of the error occurred. In the above example
  1. main called (invoked) the method readReport probably on a Payroll object
  2. which in turn called the constructor on a FileReader
  3. which in turn called the constructor on a FileInputStream
  4. which in turn called the method open on a FileInputStream
  5. where the excrement hit the fan
The bits in curved brackets tells us the name of the file the source code is in along with the line number of the instruction that was being executed.

What we are interested is where the program was in our code prior to the error - this will be the first line that mentions a file that we wrote. In the above example this is the method readReport in the file Payroll.java on line 53.

If your program catches the Exception then you will have to explicitly request a stack trace to be displyed by sending the message printStackTrace to the Exception. For example in your Java servlets your catch should be something like

    try
    {
        // ...some code in here
    }
    catch(SQLException e)
    {
        out.println("Exception when inserting books into database");
        out.println(e);  // display the message associated with exception
        e.printStackTrace(out);  // display the execution stack
        out.flush(); // ensure the messages get displayed
    }

3. Why did it occur ?

Now for the hard part - why it went wrong. Two things you should do is:

  1. simplify the line
  2. display values of variables

Simplify the line

Just as it is easier to find a needle in a shoebox of hay than in a haystack, it is easier to find the cause in a simple line than in a long complex one. So change the errant line into several simpler lines eg.
	// display age gap between child and paternal grandmother
	out.println(kid.getFather().getMother().getAge() - kid.getAge());
should become
	Person father = kid.getFather();
	Person granny = father.getMother();
	int kidAge= kid.getAge();
	int grannyAge = granny.getAge();
	int ageGap = grannyAge - kidAge;
	out.println(ageGap);

Display the values of the variables

In order to work out what is going wrong you should get the computer to display the values of variables in the alleged errant line.

Say the above code snippet gives the answer 0 for the age gap which is clearly wrong (granny is 101 and the kid is 12), then we should display the intermediate values used in the calculation.

	out.println("Kid: " + kid.toString());
	out.println("Dad: " + father.toString());
	out.println("Gran: " + granny.toString());
	out.println("kidAge="+kidAge);
	out.println("grannyAge="+grannyAge);
which could get us
	Kid: Pebbles (1st Jan 1989)
	Dad: Pebbles (1st Jan 1989)
	Gran: Pebbles (1st Jan 1989)
	kidAge=12
	grannyAge=12
which tells us that there is something wrong in the methods getMother and getFather as they are returning the child instead of the parent. Therefore we should shift our attention to them where again we can display values of variables to see which are wonky and work out why.

Another example of displaying values (in a Java Servlet that accesses a database).


If you have any comments on these webpages, please send them to: mj.hutchison@ulst.ac.uk
Copyright and Disclaimer

Last updated Monday August 6 18:51:00 BST 2001