Appendix A

Common Errors

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.

u29-01-9781785480034
Figure A-1 compile-time errors (before you press run)

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.

u29-02-9781785480034
Figure A-2 runtime errors (after you press run)

Compile-time errors

ErrorPage 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

Runtime errors

ErrorPage number
java.lang.NullPointerException530
java.lang.ArrayIndexO utOffBoundsException532

A-1 Compile-time errors

Missing a semi-colon “;”

This error means exactly what it says! You have a line of code that requires a semi missing. -colon but one is

Here are some examples (with correction bolded):

ErrorCorrected
int val = 5int val = 5;
for (int i = 0; i < 10 i + + ) {
}
for (int i = 0; i < 10; i + + ) {
}

Missing left parentheses “(“

This error means exactly what it says! You have a conditonal statement, function call, or some other line of code that requires parenthesis and you are missing one of them. Processing will tell you whether it’s left or right.

Here are some examples (same convention, bolded correction):

ErrorCorrected
if x < 5) {
background(0);
}
if (x < 5) {
background(0);
}
background0);background(0);

Missing right curly bracket “}”

You forgot to close a block of code, such as an if statement, a loop, a function, a class, and so on.

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

u29-03-9781785480034

The variable “myVar” doesn’t exist.

You are using a variable called “myVar” but it was never declared anywhere I can see.

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:

u29-04-9781785480034

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:

u29-05-9781785480034

Here is a corrected version:

u29-06-9781785480034

Or if you have declared a variable in setup(), but try to use it in draw().

u29-07-9781785480034

Corrected:

u29-08-9781785480034

The same error will occur if you use an array in any of the above scenarios.

u29-09-9781785480034

The local variable “myVar” may not have been initialized.

You declared the variable “myVar”, but you did not initialize it. You should give it a value first.

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

u29-10-9781785480034
u29-11-9781785480034

This error can also occur if you try to use an array before allocating its size properly.

u29-12-9781785480034

The class “Thing” doesn’t exist.

You are trying to declare a variable of type Thing, but there is no data type Thing. Maybe you meant to make a class called Thing?

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:

u29-13-9781785480034

Or maybe you want to create an object of type Thing, but forgot to define the thing class.

u29-14-9781785480034

This will work, of course, if you write:

u29-15-9781785480034

Finally, the error will also occur if you try to use an object from a library, but forget to import the library.

u29-16-9781785480034

In this case, if Processing can, it will provide a suggestion for which library to import.

Corrected:

u29-17-9781785480034

When possible Processing will offer import suggestions and you can click on them to automatically add the correct import statement to your code.

The function “myFunction()” expects parameters like this: myFunction(type, type, type, …)

You are calling a function incorrectly, and you need these arguments to call it correctly.

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:

u29-18-9781785480034

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.

u29-19-9781785480034

The method “function(type, type, type, …)” doesn’t exist.

You are calling a function that I have never heard of. I have no idea what you are talking about!

This is a similar error and occurs when have the wrong name for a function even if you have the correct arguments.

u29-20-9781785480034

Same goes for a function that is called on an object.

u29-21-9781785480034

Error on “_____”

I have no idea what this error is! But here’s the character that I think is causing the problem.

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.

u29-22-9781785480034

A-2 Runtime errors

java.lang.NullPointerException

I have encountered a variable whose value is null. I can’t work with this.

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

u29-23-9781785480034

Corrected:

u29-24-9781785480034

Sometimes, the same error can occur if you do initialize the object, but as a local variable by accident.

u29-25-9781785480034

The same goes for an array as well. If you forget to initialize the elements, you will get this error.

u29-26-9781785480034

Corrected:

u29-27-9781785480034

Finally, if you forget to allocate space for an array, you can also end up with this error.

u29-28-9781785480034

Corrected:

u29-29-9781785480034

java.lang.ArraylndexOutOfBoundsException: ##

You tried to access an element of an array that does not exist. (Instead of “##”, the error message will include the invalid index value.)

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.

u29-30-9781785480034

This error can be a bit harder to debug when you are using a variable for the index.

u29-31-9781785480034

A loop can also be the source of the problem.

u29-32-9781785480034
u29-33-9781785480034
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.204.208