Debugging
When you click on the Run button in the Toolbar the Processing IDE (Integrated Development Environment) compiles your sketch and starts executing the byte code output. Errors will prevent this from happening. There are 2 types of errors:
Syntax: You did not spell something correctly or you left out a character.
Logical: Everything is spelled correctly and the program runs but the output is unexpected.
Syntax Errors
Read the error message in the message area. Look at the highlighted code in the Text Editor.
Sometimes the error is easy to spot in the error message. For instance 'Cannot find anything named "studen"' indicates that the student variable was mispelled.
Sometimes the error is a little harder to see. For instance the 'Syntax error, maybe a missing semicolon' message highlights the line after the line that is missing the semicolon.
The ArrayIndexOutOf- BoundsException highlights the line that was executing when the error occured but the line that caused the error may be several lines earlier.
Re-read the example code or look at example code that is similar to your sketch.
- check for semicolons
- make sure braces are matched
Ask a friend to look at your error.
Take out the offending line or lines of code by placing two slashes, //, in front of them. Run the sketch again to make sure it is working so far. Add the lines back in one or two or as few as possible at a time.
Logic Errors
Re-read your code. Look at where the output is produced. Look at what variable might be giving the unexpected output. Trace the use of the variable backwards.
Take time to act as if you are a computer. Step through the code and write down the values of the variables at each step.
Ask a friend to look at your code. Maybe they can see something you do not.
Rem out lines of code and run again.
Add a println statement at the place you think may be the problem. Print variable values to the console and check to see if the variables are close to what you expect them to be.
You can also add text commands to your code to print variable values during execution. The advantage to this is that it keeps your eye on the Display Window and not moving back and forth to the console.
When all else fails ask your instructor.
DON'T pull your hair out agonizing over problems for hours. Getting frustrated will just lead to more errors.
Take a break. The problem might be easier to spot after a food break.
Work on other problems or assignments. Sometime inspiration comes from 'left field.'
You are not alone. Someone else may have posted a problem like yours to an online forum.
Rethink the problem. Stating the problem in your own words can be helpful whenever you start to plan out code. Maybe another approach with a fresh new start will solve the problem.
// Putting two slashes in front of a line of code turns that line into a comment. Comments are also called remarks (rems) and putting the slashes in is called "remming out" the code. Remming out code is a common debugging tool. By doing this you do not need to retype the line when you are ready to use it again - just remove the 2 slashes.
Adding good comments to your code can help you later on when debugging. Comments help you to find sections of code easily. Methods that you write should contain a 'header' with your name, date and description of the method function including the parameters.
Spaces and indenting will help you read your code. Spaces between major sections of code help to locate the block of code. Put a space before and after each new method. Use the Auto Format feature under the Edit menu to indent loops and if structures. Indenting helps the reader understand these structures.
Declare variables at the top of your sketch before setup(). Variables declared here can be used by setup(), draw() and any other methods in your sketch. Add a comment after each variable declaration to indicate what the variable does in the sketch.
eg:
int y = 0; // sets the y position of object
Set your variable values when you declare them. This can help reduce surprises later on.
Braces should be in sets, one closing brace for each open brace. Click in the space just after the brace and the Processing IDE will show you the other brace.
Processing sketches need to be saved in folders that have the same name as the sketch. When you copy or backup a sketch move the entire directory.