Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/stage0/snippets/src/Debugging.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2026 FRCSoftware
*
* SPDX-License-Identifier: BSD-3-Clause
*/

void main() {

//[logicError1]
int width = 2;
int height = 5;
int area = width + height;
System.out.println("Area of the rectangle is " + area);
//[/logicError1]



}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/config/sidebarConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export const sidebarSections: Record<string, SidebarSection[]> = {
label: 'Java Fundamentals',
slug: 'learning-course/stage0/java-fundamentals',
},
{
label: 'Debugging',
slug: 'learning-course/stage0/debugging',
},
{
label: 'Operators',
slug: 'learning-course/stage0/operators',
Expand Down
194 changes: 194 additions & 0 deletions src/content/docs/learning-course/stage0/debugging.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
---
title: Debugging
description: An Intro to Debugging
prev: learning-course/stage0/conditionals
next: false
codeRegionSources:
default: stage0/snippets/src/Debugging.java
---

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 those problems, often called bugs, in our code then fixing them.
This section will cover effective strategies to debugging your robot code.

<Aside type="note" title="Did you know?">
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".

</Aside>

# Types of Errors
Comment thread
Adrianamm marked this conversation as resolved.

There's a few different types of errors that you can run into when writing code.
Let's take a look!

<table>
<thead>
<tr>
<th>Name</th>
<th>Timing</th>
<th>Symptoms</th>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example column?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? I think because there are examples below, it's probably fine

</tr>
</thead>
<tbody>
<tr>
<td>Syntax Error</td>
<td>Compile time</td>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<td>Compile time</td>
<td>When you build your robot code in VS code</td>

<td>
The compiler will display an error message, and your code will
not finish building
</td>
</tr>
<tr>
<td>Runtime Error</td>
<td>Run time</td>
<td>
The program will crash and restart, and display an error message
</td>
</tr>
<tr>
<td>Logic Error</td>
<td>Run time</td>
<td>The code will run but not function as intended</td>
</tr>
</tbody>
</table>

## 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.

<Aside type="note">
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.
</Aside>

For instance, the following code would generate a syntax error:

<ContentFigure
src="/learning-course/stage0/debugging/syntaxError1.webp"
alt=" int number = 4; Systme.out.println(number); // <- System is misspelled!"
width="600px"
height="800px"
/>

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

<ContentFigure
src="/learning-course/stage0/debugging/syntaxError2.webp"
alt=" int number 4; // <- missing the equal sign! System.out.println(number); "
width="600px"
height="800px"
/>
Comment thread
Adrianamm marked this conversation as resolved.

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.

<ContentFigure
src="/learning-course/stage0/debugging/syntaxError3.webp"
alt=" int number = 4; System.out.println(numer); // <- number is misspelled! with numer cannot be resolved to a variable, on the top"
width="600px"
height="800px"
/>

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

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 types of runtime error is dividing by zero.

Let's look at an example.
{/* rli:ignore */}

```java
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.
Comment thread
Adrianamm marked this conversation as resolved.
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.

<Aside type="tip">
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`.

</Aside>

## 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.
Comment thread
Adrianamm marked this conversation as resolved.

Let's look at simple example

```java #logicError1

```

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the heart of the debugging tips is still missing.
Here's a good example that goes through some of the debugging steps:

You're testing your elevator code, and notice that it isn't going to the desired 'up' setpoint, though it is going back to the 'zero' position correctly. How might you debug this issue?

Step 1: Understand the system. The first step in being able to fix anything, be it software, hardware, both, or neither, is to understand how the system is designed to work. This knowledge is crucial because it allows you to compare your expectations with reality. For an FRC elevator, this includes knowing such things as:

  • How many motors are used
  • Gear ratio and drum radius
  • Elevator travel limits
  • Desired setpoint
  • Control strategy (PID, PID + feedforward, onboard vs Systemcore control, etc)
  • Is the belt tensioned properly

Trying to solve the problem without knowing these can lead to wasted time. If you didn't know the elevator travel limit, but the setpoint was above the max travel, then of course it will never get there. You might not know to check that if you were unaware of the limits.

Step 2: Check your assumptions.
The software that controls the elevator is full of assumptions for how the elevator actually works. The code may say there are 2 motors driving the carriage, but there's nothing that prevents this from being incorrect.

Once you know how the system is intended to behave, you can systematically compare your assumptions to the code's. If they are misaligned, then the resulting behavior is going to be undefined, and may result in the observed behavior.

For each assumption, try to determine what behavior would be exhibited if that assumption were wrong:

  • If the number of motors were wrong, the elevator might move slower than expected, but should still reach the setpoint
  • If the gear ratio was wrong, the elevator would think it's at its target, when in reality it would be off target
  • Incorrect travel limits would prevent the elevator from moving any further
  • If the desired setpoint was wrong, the elevator would settle off target
  • The control strategy would change the speed and smoothness of reaching the setpoint, but the elevator should still reach it
  • A loose belt would cause the position of the elevator to become out of sync with the motor's encoders, so the home position would be incorrect as well

Step 3: Compare the symptoms with the observed behavior
The gear ration and setpoint being wrong both would result in the behavior that is observed. Checking both of them, you find that the gear ratio was improperly set in code. After fixing it, the elevator goes to the right setpoint.

This process is at the heart of all debugging: Understanding the system, checking your assumptions, and then deriving the problem from the observed behavior. Not all problems are as simple as a one line gear ratio change though. What if instead, a complex algorithm, like a vision pipeline, seemed to not be working? How would you pinpoint exactly where the problem lies?

<insert more debugging info like println, unit tests, etc>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debugging page is for basic java not so much for FRC. It goes after java fundamentals so that when students get into the exercises for loops and such, they have some understand of how to fix any errors or issues that come up. That info could be helpful for stage 1 though

## 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.
Comment thread
Adrianamm marked this conversation as resolved.
- 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.

Comment thread
Adrianamm marked this conversation as resolved.
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.

- 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a dev environment setup article? This would go well there (accessibility / preferences in general)

1 change: 1 addition & 0 deletions tsconfig.tsbuildinfo

Large diffs are not rendered by default.

Loading