Appendix: Common Errors

Similar documents
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

We will start our journey into Processing with creating static images using commands available in Processing:

1 Getting started with Processing

Chapter 4 Defining Classes I

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Post Experiment Interview Questions

[ the academy_of_code] Senior Beginners

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Module 01 Processing Recap. CS 106 Winter 2018

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Lecture Notes for CS 150 Fall 2009; Version 0.5

Variables One More (but not the last) Time with feeling

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Intro. Scheme Basics. scm> 5 5. scm>

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Module 01 Processing Recap

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Learning to Program with Haiku

The compiler is spewing error messages.

Introduction to Programming Using Java (98-388)

Declarations and Access Control SCJP tips

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Finding and Fixing Bugs

Thus, to declare billy as shown above it would be something as simple as the following sentence:

Write for your audience

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS 211 Programming Practicum Spring 2017

Assignment 4: Dodo gets smarter

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

More loops Ch

BEGINNER PHP Table of Contents

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Static Methods. Why use methods?

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

CmpSci 187: Programming with Data Structures Spring 2015

CS159. Nathan Sprague

Processing Assignment Write- Ups

CSC Java Programming, Fall Java Data Types and Control Constructs

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Defensive Programming

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Lecture Topics. Administrivia

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

For instance, we can declare an array of five ints like this: int numbers[5];

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

if / if else statements

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Miscellaneous Stuff That Might Be Important.

Notes from the Boards Set # 5 Page

DCS/100 Procedural Programming

Chapter 2 Basic Elements of C++

Basic Computer Programming (Processing)

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

CMSC131. Library Classes

Simple Java Reference

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Introduction to Java. Handout-1d. cs402 - Spring

CS 231 Data Structures and Algorithms, Fall 2016

4Serial SIK BINDER //77

Exceptions and Design

CS103 Spring 2018 Mathematical Vocabulary

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

easel LANGUAGE REFERENCE MANUAL

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

c) And last but not least, there are javadoc comments. See Weiss.

Example: Computing prime numbers

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Object Oriented Programming

Expressions and Casting

CS112 Lecture: Working with Numbers

QUIZ Friends class Y;

CS 3 Introduction to Software Engineering. 3: Exceptions

Object Oriented Software Design II

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

System Error Code 1 Means Incorrect Function.

First Java Program - Output to the Screen

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Some Computer Preliminaries

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Class #1. introduction, functions, variables, conditionals

QUIZ. What is wrong with this code that uses default arguments?

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Arrays. circleracers (one racer) Object Creation and "dot syntax" Array Operation Idiom. // the x-position of the racer float x = 0;

Transcription:

Appendix: Common Errors Appendix 439 This appendix offers a brief overview of common errors that occur in Processing, what those errors mean and why they occur. The language of error messages can often be cryptic and confusing, mostly due to their origins in the Java programming language. For these error messages, we 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 specifically related to an object variable, the variable will be named myobject. If the error is related to a class, the class will be named Thing. If the error is related to a function, the function will be named function. ERROR PAGE NO. unexpected token: 440 Found one too many { characters without a to match it. 440 No accessible field named myvar was found in type Temporary_####_#### 441 The variable myvar may be accessed here before having been definitely assigned a value. 442 java.lang.nullpointerexception 442 Type Thing was not found. 444 Perhaps you wanted the overloaded version type function (type $1, type $2, ); instead? No method named fnction was found in type Temporary_####_####. However, there is an accessible method function whose name closely matches the name fnction. No accessible method with signature function (type, type, ) was found in type Temporary_3018_2848. 445 445 445 java.lang.arrayindexoutofboundsexception 446

440 Learning Processing unexpected token: something I m expecting a piece of code here that you forgot, most likely some punctuation, like a semicolon, parenthesis, curly bracket, and so on. This error is pretty much always caused by a typo. Unfortunately, the line of code that the error points to can be misleading. The something is sometimes correct and the actual error is caused by line before or after. Any time you forget or include an improper comma, semicolon, quote, parenthesis, bracket, and so on, you will get this error. Here are some examples: ERROR CORRECTED int val = 5 Missing semicolon. int val = 5; if (x < 5 { ellipse(0,0,10,10); for (int i = 0, i < 5,i + + ) { println(i); Missing close parenthesis. For loop should be separated by semicolons, not commans. if (x < 5) { ellipse (0,0,10,10); for (int i = 0; i < 5; i + +) println(i);{ Found one too many { characters without a to match it. 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 open curly bracket in your code ( { ), you must have a matching close curly bracket ( ). And since blocks of code are often nested inside each other, it is easy to forget one by accident, resulting in this error. ERRORCORRECTED for (int i = 0; i < 10; i + + ) { if (i > 5) { line(0,i,i,0); println( " i is greater than 5 "); Missing a close curly bracket! for (int i = 0; i < 10; i + + ) { if (i > 5) { line(0,i,i,0); println( " i is greater than 5 " );

Appendix 441 No accessible field named myvar was found in type Temporary_7665_1082 I do not know about the variable myvar. It is not 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: Of course, you should only declare the variable once, otherwise you can run into trouble. So this is perfectly OK: myvar = 20; OK! The variable was declared. 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: if (mousepressed) { ellipse(myvar,10,10,10); ERROR! myvar is local to the if statement so it cannot be accessed here. Here is a corrected version: int myvar = 0; if (mousepressed) { myvar = 10; ellipse(myvar,10,10,10); OK! The variable is declared outside of the if statement. Or if you have declared a variable in setup( ), but try to use it in draw( ). ellipse(myvar,10,10,10); ERROR! myvar is local to the setup() so it cannot be accessed here. int myvar = 0; myvar = 10;

442 Learning Processing ellipse(myvar,10,10,10); OK! The variable is global. The same error will occur if you use an array in any of the above scenarios. myarray[0] = 10; ERROR! No array was declared. The variable myvar may be accessed here before having been definitely assigned a value. 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 ). int myvar; line(0, myvar,0,0); line(0, myvar,0,0); ERROR! myvar does not have a value. OK! myvar equals ten. This error can also occur if you try to use an array before allocating its size properly. int[] myarray; myarray[0] = 10; ERROR! myarray was not created properly. int[] myarray = new int[3]; myarray[0] = 10; OK! myarray is an array of three integers. 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 is most commonly caused by forgetting to initialize an object. As mentioned in Chapter 8, when you declare a variable that is an object, it is 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 ). Thing thing;

Appendix 443 thing.display(); ERROR! thing was never initialized and therefore has the value null. Thing thing; thing = new Thing(); thing.display(); OK! thing is not null because it was initialized with the constructor. Sometimes, the same error can occur if you do initialize the object, but as a local variable by accident. Thing thing; Thing thing = new Thing(); thing.display(); ERROR! This line of code declares and initializes a different thing variable (even though it has the same name). It is a local variable for only setup(). The global thing is still null! The same goes for an array as well. If you forget to initialize the elements, you will get this error. Thing[] things = new Thing[10]; for (int i = 0; i < things.length; i ++ ) { things[i].display(); ERROR! All the elements on the array are null! Thing[] things = new Thing[10]; for (int i = 0; i < things.length; i ++ ) { things[i] = new Thing(); for (int i = 0; i < things.length; i ++ ) { things[i].display(); OK! The first loop initialized the elements of the array. Finally, if you forget to allocate space for an array, you can also end up with this error. int[] myarray; myarray[0] = 5; ERROR! myarray is null because you never created it and gave it a size.

444 Learning Processing int[] myarray = new int[3]; myarray[0] = 5; OK! myarray is an array of three integers. Type Thing was not found. You are trying to declare a variable of type Thing, but I do not know what a Thing is. 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: intt myvar = 10; ERROR! You probably meant to type int not intt. Or maybe you want to create an object of type Thing, but forgot to define the thing class. Thing mything = new Thing(); ERROR! If you did not define a class called Thing. This will work, of course, if you write: Thing mything = new Thing(); OK! You did declare a class called Thing. class Thing { Thing() { Finally, the error will also occur if you try to use an object from a library, but forget to import the library. Capture video = new Capture(this,320,240,30); ERROR! You forgot to import the video library. import processing.video.*; Capture video = new Capture(this,320,240,30); OK! You imported the library.

Appendix 445 Perhaps you wanted the overloaded version type function (type $1, type $2, ); instead? You are calling a function incorrectly, but I think I know what you mean to say. Did you want call this function with these arguments? 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: ellipse(100,100,50); ERROR! ellipse() requires four arguments. You will get the error: Perhaps you wanted the overloaded version void ellipse(float $1, float $2, float $3, float $4); instead? The error lists the function definition for you, indicating you need four arguments, all of floating point. The same error will also occur if you have the right number of arguments, but the wrong type. ellipse(100,100,50, "Wrong Type of Argument "); ERROR! ellipse() can not take a String! No method named fnction was found in type Temporar y_9938_7597. However, there is an accessible method function whose name closely matches the name fnction. You are calling a function I have never heard of, however, I think I know what you want to say since there is a function I have heard of that is really similar. This is a similar error and pretty much only happens when you make a typo in a function s name. elipse(100,100,50,50); ERROR! You have the right number of arguments, but you spelled ellipse incorrectly. No accessible method with signature function (type, type, ) was found in type Temporary_3018_2848. You are calling a function that I have never heard of. I have no idea what you are talking about! This error occurs if you call a function that just plain does not exist, and Processing can t make any guess as to what you meant to say. ERROR! Unless you have defined this function, Processing has no way of knowing what it is. functioncompletelymadeup(200);

446 Learning Processing Same goes for a function that is called on an object. Capture video = new Capture(this,320,240,30); video.turnpurple(); ERROR! There is no function called turnpurple() in the Capture class. java.lang.arrayindexoutofboundsexception: ## You tried to access an element of an array that does not exist. 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. int[] myarray = new int[10]; myarray[-1] = 0 myarray[0] = 0; myarray[5] = 0; myarray[10] = 0; myarray[20] = 0; ERROR! 1 is not a valid index. OK! 0 and 5 are valid indices. ERROR! 10 is not a valid index. ERROR! 20 is not a valid index. This error can be a bit harder to debug when you are using a variable for the index. int[] myarray = new int[100]; myarray[mousex] = 0; ERROR! mousex could be bigger than 99. int index = constrain(mousex,0,myarray.length-1); myarray[index] = 0; OK! mousex is constrained between 0 and 99 first. A loop can also be the source of the problem. for (int i = 0; i < 200; i + + ) { myarray[i] = 0; ERROR! i loops past 99. for (int i = 0; i < myarray.length; i + + ) { myarray[i] = 0; OK! Using the length property of the array as the exit condition for the loop. for (int i = 0; i < 200; i + + ) if (i < myarray.length) { myarray[i] = 0; { OK! If your loop really needs to go to 200, then you could build in an if statement inside the loop.