Debugging
When completing the exercise for Java Fundamentals, you probably ran into errors and then fixed them. This means that you did some debugging! Debugging is the process of finding errors, often called bugs, then fixing them. This section will cover effective strategies to debug your robot code.
Using the word “bug” to refer to an error in a program was popularized by Grace Hopper, an American computer scientist. While her and her team was working on the Mark II computer, they were able to trace an error to moth that was stuck in the computer. They put the moth in their notebook with the note “First actual case of bug being found”.
Types of Errors
Section titled “Types of Errors”There’s a few different types of errors that you can run into when writing code. Let’s take a look!
| Name | Timing | Symptoms |
|---|---|---|
| Syntax Error | Compile time | The compiler will display an error message, and your code will not finish building |
| Runtime Error | Run time | The program will crash and restart, and display an error message |
| Logic Error | Run time | The code will run but not function as intended |
Syntax Error
Section titled “Syntax Error”As part of the build process, there’s a program called a compiler that takes all the code you wrote and turns it into instructions a machine will understand.
Just like English has grammar rules, Java also has a grammar to ensure the compiler can understand the code you’ve written. If you don’t follow this grammar, the compiler will complain by giving an error. VS Code shows these errors by putting a red line under the part of code that it thinks is wrong, just like a typo in Google Docs.
This error is called a syntax error because these errors occur when you mess up the syntax of a language. We say this error happens at compile time because it happens when the compiler is translating the code, before it ever actually runs.
When programming, a yellow line might appear under a part of code. Most of the time, this is not an error. It’s a warning and it typically does not affect the output of the code. However, you should pay close attention to the error as it might cause undesired outputs.
For instance, the following code would generate a syntax error:

Misspelling variable names is also a syntax error. Not using proper syntax, as shown, also generates a syntax error.

If you notice that the code has the red line under it, hover over that line of code.
VS Code will give more information about the error.
Let’s look back at the previous misspelled variable name example,
when putting the cursor over the error, it reads numer cannot be resolved to a variable.
This means it’s looking for a variable called numer but it can’t find it.

When the code runs, the error message can also appear in the terminal. This message is called a stacktrace. A stacktrace traces or identifies the spot in the program where the error happened.
Sometimes, errors are too subtle for the compiler to notice immediately, and it may look like a different part of code is causing the error. Therefore, it’s important to read the stacktrace carefully to try to understand it is saying before trying to fix the error.
Runtime Error
Section titled “Runtime Error”The code might look like it has no bugs, but when the code runs, an error appears. When this happens, it’s a runtime error. Runtime errors are errors that can only be found when the program is running. One common type of runtime error is dividing by zero.
Let’s look at an example.
int answer = 10 / 0; System.out.println(answer);The example looks like it should run.
However, if the code was to run, it would give a runtime error for Diving by zero.
The terminal will show an error similar to Exception in thread "main" java.lang.ArithmeticException: / by zero at main.main(main.java:5)
By reading the error message, it’s clear that the error is a divide by 0 error.
This happens in int answer = 10/0; because it’s diving by zero.
One way to fix this error would be to swap the 10 and 0.
The stacktrace will tell you what line the error is on.
For example, Exception in thread "main" java.lang.ArithmeticException: / by zero at main.main(main.java:5)
has (main.java:5).
That means the error is on line 5 of main.java.
Logic Error
Section titled “Logic Error”There will be times when the code runs, has no errors but it doesn’t give the desired output. Computers don’t know what we are expecting them to do. They only know what we told them. If the instructions given are wrong, then the output will be wrong too.
Let’s look at simple example
int width = 2;int height = 5;int area = width + height;System.out.println("Area of the rectangle is " + area);This example finds the area of a rectangle.
The width is 2 and the height is 5.
If this code was to be ran, it would print out Area of the rectangle is 7, but that isn’t right.
The area should be 10.
The code runs with no errors, so why it the code doing the math wrong?
When trying to find the area of a rectangle, the equation for the area is width times height.
In the code, it isn’t multiplying the width and height, it adds the two.
Therefore, changing the addition sign to a multiplication sign will fix the issue and it should print out Area of the rectangle is 10.
This is a simple example of a logic issue.
The code ran with no errors but, but had bad instructions.
Software bugs are often not as easy to spot as this example.
Be sure to take time to read through the code and verify that the intended behavior matches what the code is saying.
Adding System.out.println(); can also help break down what the code is doing at different spots in the program.
Helpful Tips
Section titled “Helpful Tips”Errors will always appear in code, but there are things to do to help avoid errors appear as often.
- Use variable names that match their meaning.
- Split up complex logic blocks into smaller, simpler ones.
- Keep the code easy to read. Readability is important.
- Code is made up of building blocks. Test the smaller blocks so that you’re sure your foundation is solid for the larger blocks.
When there is an error, read through the error message carefully. If you’re stuck, use online resources! Looking up an error can help point you to the right direction on how to fix it.
- VS Code has a bunch of accessibility features which can be helpful for debugging and writing code. More information can be found online.
- For some, changing VS Code to be in a more accessible font (i.e Comic Sans) can be helpful in reading code to find or avoid errors.