This appendix offers a brief overview of common errors that occur in Processing, what those errors mean and why they occur. Whenever possible, Processing attempts to rephrase an error message to be clear and friendly, however the language of error messages is still often cryptic and confusing, mostly due to their origins in the Java programming language.
For these error messages, I will use the following conventions:
• Variables in the error messages will be named myVar.
• Arrays in the error messages will be named myArray.
• If the error is related to a class, the class will be named Thing.
• If the error is specifically related to an object variable, the variable will instead be named myThing.
• If the error is related to a function, the function will be named myFunction.
There is also another important distinction to be made regarding errors. Some errors are syntax errors and stop the program from starting. These are shown with a red squiggly line under text in the editor window as well as listed in the error console at the bottom. These errors are often called “compile-time” errors since they are caught at the moment Processing attempts to compile the code.
Another category is “runtime” errors. These are errors that are not caught before the code is run but rather occur due to a flaw or bug in the program itself. These are often harder to pinpoint though some tips are offered in Chapter 11. In Java these errors are exceptions as covered in Section 23-6 on page 518.
Error | Page number |
Missing a semi-colon “;” | 525 |
Missing left parentheses “(“ | 525 |
Missing right curly bracket “}” | 526 |
The variable “myVar” doesn’t exist. | 526 |
The local variable “myVar” may not have been initialized. | 527 |
The class “Thing” doesn’t exist. | 528 |
The function “myFunction()” expects parameters like this: myFunction(type, type, type, …) | 529 |
The method “myFunction(type, type, type, …)” does not exist. | 529 |
Error on “ “ | 530 |
Here are some examples (with correction bolded):
Here are some examples (same convention, bolded correction):
Any time you have an opening (a.k.a. left) curly bracket in your code (“{“), you must have a matching closing (a.k.a right) curly bracket (“}”). And since blocks of code are often nested inside each other, it’s easy to forget one by accident, resulting in this error. Processing will typically let you know whether you are missing a left or right curly bracket but in some cases when it’s confused it may just say “Error on }”.
This error will happen if you do not declare a variable. Remember, you can’t use a variable until you have said what type it should be. You will get the error if you write:
myVar = 10;
instead of:
int myVar = 10;
Of course, you should only declare the variable once, otherwise you can run into trouble. So this is perfectly OK:
This error can also occur if you try to use a local variable outside of the block of code where it was declared. For example:
Here is a corrected version:
Or if you have declared a variable in setup(), but try to use it in draw().
Corrected:
The same error will occur if you use an array in any of the above scenarios.
This is a pretty simple error to fix. Most likely, you just forgot to give your variable its default starting value. This error only occurs for local variables (with global variables Processing will either not care, and assume the value is zero, or else throw a NullPointerException).
This error can also occur if you try to use an array before allocating its size properly.
This error occurs because you either (a) forgot to define a class called Thing or (b) made a typo in the variable type.
A typo is pretty common:
Or maybe you want to create an object of type Thing, but forgot to define the thing class.
This will work, of course, if you write:
Finally, the error will also occur if you try to use an object from a library, but forget to import the library.
In this case, if Processing can, it will provide a suggestion for which library to import.
Corrected:
When possible Processing will offer import suggestions and you can click on them to automatically add the correct import statement to your code.
This error occurs most often when you call a function with the incorrect number of arguments. For example, to draw an ellipse, you need an x location, y location, width, and height. But if you write:
You will get the error: “The function “ellipse()” expects parameters like this: ellipse(float, float, float, float)” The error provides the function signature, indicating you need four arguments, all floating point. The same error will also occur if you have the right number of arguments, but the wrong type.
This is a similar error and occurs when have the wrong name for a function even if you have the correct arguments.
Same goes for a function that is called on an object.
When Processing cannot give you a detailed error message it just points out the line of code and character where it thinks the error is occuring. It usually gets it right, but you may want to check the line above and below just in case too. The most common cause of this error message is a stray character that is invalid.
The NullPointerException error can be one of the most difficult errors to fix. It’s most commonly caused by forgetting to initialize an object. As mentioned in Chapter 8, when you declare a variable that is an object, it’s first given the value of null, meaning nothing. (This does not apply to primitives, such as integers and floats.) If you attempt to then use that variable without having initialized it (by calling the constructor), this error will occur. Here are a few examples (that assume the existence of a class called Thing).
Corrected:
Sometimes, the same error can occur if you do initialize the object, but as a local variable by accident.
The same goes for an array as well. If you forget to initialize the elements, you will get this error.
Corrected:
Finally, if you forget to allocate space for an array, you can also end up with this error.
Corrected:
This error will happen when the index value of any array is invalid. For example, if your array is of length 10, the only valid indices are zero through nine. Anything less than zero or greater than nine will produce this error.
This error can be a bit harder to debug when you are using a variable for the index.
A loop can also be the source of the problem.
3.138.204.208