Debugging Java Code : Debugging Java Code Frances Sheridan, Computing Support Tutor
01 6599260
fsheridan@ncirl.ie
Types of Programming Errors : Types of Programming Errors Compile-time – Errors that prevent your code from compiling
Lexical – variable name starting with a number
Syntactical – Unbalanced { }
Semantic – two variables with the same name
Logic – Program compiles but does not do what you want it to do
Run-time – When your code compiles but errors are thrown during the running of your program
Before You Start Debugging : Before You Start Debugging Format all code according to programming guidelines
Display line numbers
Remember:
Java executes code line by line unless told otherwise
A single line of code has caused your program to crash
This may not be the line that needs to be changed. The line that was the cause of the error may not be the reason for it
You may have 55 million errors but you should always start with error number 1!
Debugging - 3 Steps : Debugging - 3 Steps Read the error message carefully (if one exists)
Think critically about the code
What does this error mean?
What objects are concerned?
Have I declared, created and initialised all of the necessary components?
If that fails, isolate the problem
Isolating the Problem : Isolating the Problem System.out.println();
add print statements at various points reporting the status of the program such as the values of variables
make enough information visible but not too much
Label the information that you are printing
i.e. System.out.println(“Value of count after loop:”+count);
Isolating the Problem : Isolating the Problem Comments
Comment large parts of code initially to eliminate the error
Begin narrowing your scope by uncommenting code line by line until the error reappears