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
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
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)
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
main called (invoked) the method
readReport
probably on a Payroll object
FileReader
FileInputStream
open on
a FileInputStream
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
}
Now for the hard part - why it went wrong. Two things you should do is:
// 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);
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=12which 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