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 › Odd error with classes in "Java mode"
Page Index Toggle Pages: 1
Odd error with classes in "Java mode" (Read 654 times)
Odd error with classes in "Java mode"
May 16th, 2007, 12:36am
 
The following test code throws an exception at runtime:

public class Test extends PApplet {
 void setup()
 {
   MyType t = new MyType();
 }
}

public class MyType{
 public MyType(){
 }
}

Error:
java.lang.ClassCastException
at processing.core.PApplet.main(PApplet.java:6606)

Am I doing something wrong?
Re: Odd error with classes in "Java mode"
Reply #1 - May 17th, 2007, 4:19pm
 
Can anyone replicate this?
Re: Odd error with classes in "Java mode"
Reply #2 - May 17th, 2007, 4:46pm
 
Hi diordna,

I got the same error message when running your code. I didn't get the error message when I first called the setup and draw method (empty).

You should not call setup() more than once in your program though; I don't know what a (second) setup call in the Test class will do.

/BEnm
Re: Odd error with classes in "Java mode"
Reply #3 - May 18th, 2007, 5:54am
 
diordna,
I think you might be confusing syntax from Processing's continuous mode and Java mode.

In continuous mode, classes are inner classes-the class declaration happens within another class.

In Java mode, you'd stick you're MyType class in another tab and name it MyType.java.

Here's your example in continuous mode (notice I got rid of the Java mode syntax):

Code:

void setup(){
MyType t = new MyType();
}

class MyType{
MyType(){
}
}


Here it is in Java mode:
Code:

public class Test extends PApplet {
void setup(){
MyType t = new MyType();
}
}

You need another tab, named "MyType.java", which needs to include (at least):
Code:

public class MyType{
public MyType(){
}
}


Although, you can also treat MyType as an inner class in Java mode:

Code:

// Java mode with inner class
public class Test extends PApplet {
void setup(){
MyType t = new MyType();
}

//inner class
class MyType{
MyType(){
}
}
}





Re: Odd error with classes in "Java mode"
Reply #4 - May 18th, 2007, 4:21pm
 
I got the same error when I put it in a different file.

Edit: Now I get it. I didn't realize you had to add a ".java" extension, and I didn't see it in the docs anywhere.
Re: Odd error with classes in "Java mode"
Reply #5 - May 18th, 2007, 6:55pm
 
see the reference for "sketchbook" and "tabs (multiple files)" for more info:
http://processing.org/reference/environment/index.html
Page Index Toggle Pages: 1