π Debugging Errors
Code not working? Follow the steps in the debugging process below before asking a peer or your teacher! Fixing your own errors, no matter how small, is one of the best ways to become a better coder.
β Java Debugging Process
-
Read the Error Message
- Focus on the first error, not the dozens that follow β one small bug can cause many.
- Look at the line number and the type of error (e.g.,
SyntaxError
,NullPointerException
).
-
Check for Missing Symbols
- Every line (except the last in a block) needs a semicolon (
;
). - Make sure parentheses
()
, braces{}
, and brackets[]
are all paired. - Method headers must have parentheses:
public void sayHi()
- Every line (except the last in a block) needs a semicolon (
-
Check Braces and Indentation
- Every
{
must have a matching}
. - Indent code inside
{}
to match visually. - Donβt forget to close classes and methods!
- Every
-
Check Capitalization
- Java is case-sensitive:
System
βsystem
,main
βMain
- Classes must start with capital letters (
MyClass
), but variables/methods use lowercase (myVariable
)
- Java is case-sensitive:
-
Check Spelling and Naming
- Watch for typos like
Systm.out.print
instead ofSystem.out.print
- Match variable names exactly; Java wonβt correct you
- Watch for typos like
-
Check Data Types and Declarations
- Make sure all variables are declared with a type (
int
,String
,double
, etc.) - Strings must be capitalized:
String name = "Alice";
- Make sure all variables are declared with a type (
-
Print for Clarity
- Use
System.out.println()
to check variable values - Print before and after key lines to isolate bugs
- Use
-
Compile (Press RUN) Often
- Donβt write 50 lines without compiling. Compile frequently so you catch errors early.
-
Ask Yourself Questions
- Is this variable initialized?
- Am I using the correct data type for this operation?
- Should this be inside the
main
method?
-
If All Else Failsβ¦
- Comment out blocks to isolate the issue
- Use a visualization tool like PythonTutor.com to βstep throughβ code, which shows whatβs happening line-by-line.