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 › Unexpected token
Page Index Toggle Pages: 1
Unexpected token (Read 2177 times)
Unexpected token
Dec 11th, 2009, 2:10pm
 
Howdy;

I'm a reasonably experienced programmer who's new to processing. I'm encountering a very frustrating error that occurred only after I tried to take some classes out of the original processing file (which works) and put them into their own files.

This code is posted below. As per my description above, basically all I've done is put each of the classes in their own file, and add one line to the main file:

Code:
void setup() {} 



When I attempt to run this code, I get this message:

Code:
processing.app.debug.RunnerException: unexpected token: ( 



I haven't been able to figure out much about where this is coming from, other than that it happens whenever I attempt to call a method of one of the classes that I put in the other files. Any help on this would be very much appreciated!

Code:
// SCRIPT

Axis xa = new Axis(400, 0, 0, 30, "test X");
Axis ya = new Axis(30, 0, 0, 5, "test Y");
Plot p = new Plot(1500, 800, 30, "test plot");
p.setAxes(xa, ya);
p.plot();

// CLASSES

// Axis class: used to define individual axes
class Axis {
 
 // attributes
 float aMax, aMin, aInt, aTick; // axis min, max, location of intersection with other axis, distance between ticks
                               // (in axis units)
 String aLabel; // label to be displayed on plot
 
 // constructor method
 Axis(float ax, float in, float nt, float ti, String lab) {
   aMax = ax;
   aMin = in;
   aInt = nt;
   aTick = ti;
   aLabel = lab;
 }
 

}

// Axes class: used to define a set of axes for the plot
class Axes {

 // attributes
 Axis yAxis, xAxis; //axes
 
 // constructor method
 Axes(Axis x, Axis y) {
   xAxis = x;
   yAxis = y;
 }

 // plot method
 void plot(int xDim, int yDim, int margin) {
// etc, etc... code deleted here to fit 6000 char limit!
 }
 } // plot method
} // Axes class

// Point class: used to define individual points to be plotted
class Point {
 
 // attributes
 float xVal, yVal; // x and y value of point
 float nArm, sArm, eArm, wArm; // values of each "arm" of point; this is very specific to glazing ratio application
 float armSize = 20; // value by which glazing ratios are divided when determing a point's "arm" length, in pixels

 // constructor method
 Point(float x, float y, float n, float s, float e, float w) {
   xVal = x;
   yVal = y;
   nArm = n;
   sArm = s;
   eArm = e;
   wArm = w;
 }
 
 // other methods
 void plot() {
   //point(put black point at actual value location, then coloured arms from point)
 }
 
}

// Plot class: this is where all of the plot data lives
class Plot {
 
 // attributes
 int xSize, ySize, margin; // x and y size of plot, margin size, all in pixels
 String title; // title of plot
 Axes pAxes; // axes
 Point[] points; // array of Point objects to be plotted
 
 // constructor method
 Plot(int x, int y, int m, String t) {
   xSize = x;
   ySize = y;
   margin = m;  
   title = t;
 }
 
 // other methods
 
 // set axes method
 void setAxes(Axis x, Axis y) {
   Axes a = new Axes (x, y);
   pAxes = a;
 }
 
 // plot method
 void plot() {
   // prepare window
   size(xSize, ySize);
   background(255,255,255);
   
   // load font
   PFont font;
   font = loadFont("Calibri-12.vlw");
   textFont(font);
   fill(0);
   
   // plot axes
   pAxes.plot(xSize, ySize, margin);
 }
 
}  





Re: Unexpected token
Reply #1 - Dec 11th, 2009, 2:21pm
 
Your Axes class has got one too many closing parentheses }, just after the plot method...  That's the sort of thing that commonly leads to an 'unexpected token' error.  Also - how did you create the separate files?  Presumably you know that adding a tab in the IDE saves the contents as a separate file?
Re: Unexpected token
Reply #2 - Dec 11th, 2009, 2:26pm
 
You also appear to be missing setup() and draw() methods - or are these in another file  Note these are essential to run any kind of complex code in Processing.  See Environment > modes; though TBH the explanation is a little unsatisfactory.  If you search the site you'll find that Philho has answered the question plenty of times already Wink
Re: Unexpected token
Reply #3 - Dec 11th, 2009, 2:33pm
 
Thanks for the reply.

Sorry, I had to cut a bunch of code out of the Axes class to get everything to git within the 6000 character limit of the forum. The missing opening { is in the code I cut out.

To create the files I did as you describe: I created a new tab, cut the code from the main file into the tab, and saved both.
Re: Unexpected token
Reply #4 - Dec 11th, 2009, 2:36pm
 
I wasn't using the "draw" method, because my image is meant to be static. Do I need to use it if I'm just trying to create a static image?
Re: Unexpected token
Reply #5 - Dec 11th, 2009, 3:03pm
 
So everything just gets calculated once and drawn once?  Then in theory I guess you could probably get away without the need for draw(); but there's no harm in adding in an empty draw and testing.  One definite issue some people come across is not putting size() as the first call in setup().  In certain situations that breaks your programme.

I could be wrong but I'm sure I've also run into problems when moving classes to a new tab which have been solved by closing down Processing and starting it up again (but that could just be coincidence).

Still I'm sorry to say that the problem could well be a missing parentheses shortly followed by a (
In fact maybe a ( instead of a {?

Unfortunately when classes get put into separate tabs, in my experience, Processing doesn't always do a good job of highlighting the offending line and you have to hunt it down manually.  I'll sometimes copy paste all my code into a separate text editor with code folding and hunt it down that way...
Re: Unexpected token
Reply #6 - Dec 12th, 2009, 2:09am
 
Josh K wrote on Dec 11th, 2009, 2:36pm:
I wasn't using the "draw" method, because my image is meant to be static. Do I need to use it if I'm just trying to create a static image

No.
Unless you need to manage user input (keyboard, mouse): in this case, draw() is necessary, but you can use it with noLoop().
Re: Unexpected token
Reply #7 - Dec 15th, 2009, 10:33am
 
Okay, I fixed it.

Previously I had the size(), background(), and font loading stuff in the Plot class. I took this out and stuck it within the setup() method, to which I also added a noLoop().

Then I took the "script" portion of the code (which was initially the only code in the "parent" .pde file, and stuck it into draw(), like so:

Code:

void setup() {
 // prepare window
 size(1100, 800);
 background(255,255,255);
 
 // load font
 PFont font;
 font = loadFont("Calibri-12.vlw");
 textFont(font);
 fill(0);
 
 //draw options
 noLoop();
 smooth();
}

void draw() {
 Axis xa = new Axis(400, 0, 0, 30, "test X");
 Axis ya = new Axis(30, 0, 0, 5, "test Y");
 Plot p = new Plot(1100, 800, 30, "test plot");
 p.setAxes(xa, ya);
 p.plot();
}


So, it works now -- thanks a lot for the responses. However, I must say, this does seem a little bit arbitrary...
Page Index Toggle Pages: 1