We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › And yet the constructor is defined...
Page Index Toggle Pages: 1
And yet the constructor is defined... (Read 417 times)
And yet the constructor is defined...
Jun 9th, 2009, 12:37pm
 
Hi everyone !

When trying to launch the program, Processing says "Sorry mate but the "Stick" constructor is not defined !".

Well, hell it is !

Do you have any idea ? Maybe another syntax error that gets him mixed up...

The bonus question is : why do I have to "announce" the objects I'm creating at the very top of the page? What if I have a conditional creation?

Code:

Stick stick1;

void setup() {
stick1 = new Stick("", "", 10, 10, 20, 20);
}

void draw() {
size(640, 360);
background(255);
}

class Stick {
// The color of the stick
color colour;
// The thickness of the stick
int thickness;
// How stretchable the stick is
int stretch;
// The stick first coordinates
int acoord[];
// The stick second coordinates
int bcoord[];
// How "sticky" the stick is
int stickiness;
// Its weight
int weight;
// The A end (first coordinates) attached stick (another stick's name)
char ahandler;
// The B end attached stick
char bhandler;
// Wether the A "spring" elbow attracts (A) or repeals (R) or is loose (L)
char aspring;
// Same for the B "spring"
char bspring;
// An array containing the names of the elements it can pierce through. The other ones block it
char[] through;

Stick(char t_ahandler, char t_bhandler, int t_acoordX, int t_acoordY, int t_bcoordX, int t_bcoordY) {
colour = #000000;
thickness = 1;
ahandler = t_ahandler;
bhandler = t_bhandler;
acoord[0] = t_acoordX;
acoord[1] = t_acoordY;
bcoord[0] = t_bcoordX;
bcoord[1] = t_bcoordY;
line(acoord[0], acoord[1], bcoord[0], bcoord[1]);
}
}


Thanks for your time  Wink
Re: And yet the constructor is defined...
Reply #1 - Jun 9th, 2009, 12:56pm
 
It is expecting a char ('a') and you are passing strings ("a"). It is a picky language. Smiley

Without "announcing" variables, Java does not know what you are referring to.

When you announce an int, Java makes a spot in memory for it.

When you announce an Object (such as a Stick) Java makes a spot for its reference.

Then, when you call the constructor, more memory is taken for the contents of the Object.

A couple things to look out for:
Java is also picky about passing null characters: '' versus ' ' (nothing between the first)
You will also want to put size() inside setup instead of draw().

Hope that helps.
Re: And yet the constructor is defined...
Reply #2 - Jun 9th, 2009, 12:56pm
 
You have defined the constructor to accept two chars, plus four ints.

Yet, when instantiating the object, you are trying to pass it two Strings and four ints.

Either change the constructor to take Strings (not chars), or feed it chars (not Strings).  Processing will interpret a null "" as a String.  (See reference for char datatype.)

As for the bonus, you only have to "announce" an object at the top if you want to use reference it throughout the sketch.  Object scope works the same as variable scope -- so if you declare it "at the top," you can access/manipulate it everywhere later, e.g. in setup(), draw(), and other functions.
Constructor and Arrays newbie problems
Reply #3 - Jun 9th, 2009, 1:29pm
 
Ho yes Noah it's a picky language! I've used PHP and JS and none care about that difference Smiley

OK so I've corrected the string problem. Thanks to you two.

About the bonus question : If I don't annouce my Object at the top, Processing stops at the line 9... :

Code:
stick1 = new Stick("", "", 10, 10, 20, 20); 



...arguing that it cannot find anything names stick1.

-____-

Well guess what Processing, I'm creating the object you're talkin' about!

So I understand the scope difference but not why Processing acts so dull with me  Undecided

Also, I get an error about line 43 and I think the following 3 will do the same. "Null pointer". Is there something wrong with the way I set my arrays up?

The corrected code :
Code:
Stick stick1;

void setup() {
size(640, 360);
}

void draw() {
background(255);
stick1 = new Stick("", "", 10, 10, 20, 20);
}

class Stick {
// The color of the stick
color colour;
// The thickness of the stick
int thickness;
// How stretchable the stick is
int stretch;
// The stick first coordinates
int[] acoord;
// The stick second coordinates
int[] bcoord;
// How "sticky" the stick is
int stickiness;
// Its weight
int weight;
// The A end (first coordinates) attached stick (another stick's name)
String ahandler;
// The B end attached stick
String bhandler;
// Wether the A "spring" elbow attracts (A) or repeals (R) or is loose (L)
char aspring;
// Same for the B "spring"
char bspring;
// An array containing the names of the elements it can pierce through. The other ones block it
char[] through;

Stick(String t_ahandler, String t_bhandler, int t_acoordX, int t_acoordY, int t_bcoordX, int t_bcoordY) {
colour = #000000;
thickness = 1;
ahandler = t_ahandler;
bhandler = t_bhandler;
acoord[0] = t_acoordX;
acoord[1] = t_acoordY;
bcoord[0] = t_bcoordX;
bcoord[1] = t_bcoordY;
line(acoord[0], acoord[1], bcoord[0], bcoord[1]);
}
}

Re: Constructor and Arrays newbie problems
Reply #4 - Jun 9th, 2009, 1:45pm
 
Rob Mayol wrote on Jun 9th, 2009, 1:29pm:
If I don't annouce my Object at the top, Processing stops at the line 9...  [...] ...arguing that it cannot find anything names stick1. [...] Well guess what Processing, I'm creating the object you're talkin' about!

Re-read the given explanations. Java is still picky, you have to declare variables before defining them, so that Java will allocate some space (actually just a reference for objects) of the right type (no dynamic typing in Java!). If you remove the declaration, Java will be lost...

Quote:
"Null pointer". Is there something wrong with the way I set my arrays up

Good guess.
Another difference: Java arrays are immutable, ie. they won't grow when you put stuff in them (you can use ArrayList class for that behavior). So you have to allocate them before using them: int[] bcoord = new int[2]; Although for such small array, you could use just a pair of integers...
Re: Constructor and Arrays newbie problems
Reply #5 - Jun 9th, 2009, 1:51pm
 
PhiLho  wrote on Jun 9th, 2009, 1:45pm:
Re-read the given explanations. Java is still picky, you have to declare variables before defining them, so that Java will allocate some space (actually just a reference for objects) of the right type (no dynamic typing in Java!). If you remove the declaration, Java will be lost...


OK I get it now, or at least accept it  Wink

So every object has to be created when coding What if I want to make a game where you can create a player for example Do I have to hard code x player objects and the game will never be able to have x+1 players until I code it
Re: And yet the constructor is defined...
Reply #6 - Jun 9th, 2009, 2:40pm
 
You can use things like ArrayList (or any Collection) to add objects to on the fly.
Code:
ArrayList al = new ArrayList(); //you do need to make this one.
...
al.add( new Stick(...) ); //Added a new Stick object without another reference to it
...
//to use that new Stick
Stick stickInHand = (Stick)al.get(indexOfMyFavoriteStick);

http://processing.org/reference/ArrayList.html

You could do this manually with arrays (meaning the background stuff that ArrayList does), but it is tedious work.
Re: And yet the constructor is defined...
Reply #7 - Jun 9th, 2009, 3:38pm
 
That syntax is so weird :
Quote:
(Stick)al.get(indexOfMyFavoriteStick);


Is there a reason why Stick is in brackets or is it just something I have to get along with. Like God or whatever Smiley

Thank you so much once again, to everyone, I'm not used to see boards where you're not affraid to ask too much. Hope I'm not though Wink
Re: And yet the constructor is defined...
Reply #8 - Jun 9th, 2009, 4:30pm
 
The (Stick) is for casting. al.get() returns an Object.

By casting to Stick, Java knows what properties it has.

Edit: I had it backwards before.

If you assign
Stick astick = al.get(i); it will tell you that it is expecting a Stick object.
So casting is needed.

If you do
Object ob = new Stick(...);

It won't have any trouble until you try to access a Stick-specific property. You could also cast ob to a Stick, though.
Re: And yet the constructor is defined...
Reply #9 - Jun 9th, 2009, 10:29pm
 
Rob Mayol wrote on Jun 9th, 2009, 3:38pm:
That syntax is so weird

Yeah, I was just used to it because I code C for a long time...
As I wrote, Java is statically typed and quite strict about it. The only way to cheat is to declare an object as Object, which is the base class for everything (you can declare a variable with a quite generic type and put there a more specialized object as long as it is derived from the original generic type).

In Java 1.5 (but you cannot do that yet in Processing), you can declare before hand the type of the objects the ArrayList will hold, so you don't need to cast them on the way out:
Code:
ArrayList<Stick> al = new ArrayList<Stick>();
...
al.add( new Stick(...) );
...
Stick stickInHand = al.get(indexOfMyFavoriteStick);

Yeah, syntax is still quite ugly and verbose... Smiley
Page Index Toggle Pages: 1