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 › OOP, ArrayIndexOutOfBoundsException: 0
Page Index Toggle Pages: 1
OOP, ArrayIndexOutOfBoundsException: 0 (Read 890 times)
OOP, ArrayIndexOutOfBoundsException: 0
Jan 24th, 2009, 7:48pm
 
Hi,
  I'm pretty new to OOP, and I'm trying to build my first working(non-procedural) program in processing.  I have a program with two classes; the first initiates an array containing an attractor set.  I'm troubleshooting, and I've come across the following error which I believe has something to do with the locality of the array?

Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 0
at att_sphere$attractor.newSet(att_sphere.java:52)
at att_sphere.setup(att_sphere.java:29)
at processing.core.PApplet.handleDraw(PApplet.java:1383)
at processing.core.PApplet.run(PApplet.java:1311)
at java.lang.Thread.run(Thread.java:613)

Code:

sph2cart s2c;
cart2sph c2s;
attractor set1;


void setup(){
s2c = new sph2cart();
c2s = new cart2sph();
set1 = new attractor();
set1.newSet(1024);
set1.popSet(1.1,-1,1,1.5,1.1,-1);
}


class attractor{
// float a,b,c,d,e,f;
int points;
double bigX,bigY,bigZ;
float[]x = new float[points];
float[]y = new float[points];
float[]z = new float[points];
float[][]xyz = {x,y,z};

attractor(){



}

void newSet(int objNo){
points = objNo;

xyz[0][0] = 0;
xyz[1][0] = 0;
xyz[2][0] = 0;
println(points);
}

void popSet(float a, float b, float c, float d, float e, float f){
int n;
for (int i=0; i<points; i++){
n = (i+1)%points;


//xyz[0][n]//
bigX = (Math.sin(a*xyz[1][i]))+ (c*(Math.cos(a*xyz[0][i])));
//xyz[1][n]//
bigY = (Math.sin(b*xyz[0][i]))+ (d*(Math.cos(b*xyz[1][i])));
//xyz[2][n]//
bigZ = (Math.sin(e*xyz[1][i]))+ (f*(Math.cos(f*xyz[1][i])));
xyz[0][n] = (float)bigX;
xyz[1][n] = (float)bigY;
xyz[2][n] = (float)bigZ;

}
}

float getX(int i){
return(xyz[0][i]);
}
float getY(int i){
return(xyz[1][i]);
}
float getZ(int i){
return(xyz[2][i]);
}

}


Thanks for your help,
Ryan Nestor.
Re: OOP, ArrayIndexOutOfBoundsException: 0
Reply #1 - Jan 24th, 2009, 8:03pm
 
The problem is here:

Code:

class attractor{
 // float a,b,c,d,e,f;
 int points;
 double bigX,bigY,bigZ;
 float[]x = new float[points];
 float[]y = new float[points];
 float[]z = new float[points];


This initializes the arrays x, y, z with points, which has a default value of 0 (this will happen to int if you do not explicitly state the initial value).


This will probably need to be done inside newSet().
Re: OOP, ArrayIndexOutOfBoundsException: 0
Reply #2 - Jan 24th, 2009, 8:06pm
 
When you write:
Code:
    int points;
   double bigX,bigY,bigZ;
   float[]x = new float[points];
   float[]y = new float[points];
   float[]z = new float[points];
   float[][]xyz = {x,y,z};

the arrays are created when you spawn a new instance of the class, before the constructor is called, even more before newSet() call.
You should do these inits in the constructor or in newSet, after points value is known (it is 0 by default).

[EDIT] Too late! Wink
Re: OOP, ArrayIndexOutOfBoundsException: 0
Reply #3 - Jan 24th, 2009, 8:17pm
 
yeah, this is what I initially tried.  However, when my method popSet tried to populate the array, i got:  can't find anything named "xyz"


Code:

class attractor{
// float a,b,c,d,e,f;
int points;

attractor(){

double bigX,bigY,bigZ;
float[]x = new float[points];
float[]y = new float[points];
float[]z = new float[points];
float[][]xyz = {x,y,z};
}

//etc...
Re: OOP, ArrayIndexOutOfBoundsException: 0
Reply #4 - Jan 24th, 2009, 8:56pm
 
I was waiting for this one... Smiley
You must separate the declarations from the initializations.
Code:
class attractor{
// float a,b,c,d,e,f;
int points;
double bigX,bigY,bigZ;
float[]x;
float[]y;
float[]z;
float[][]xyz = {x,y,z};

attractor(int pointNb){
points = pointNb;
x = new float[points];
y = new float[points];
z = new float[points];
}
Re: OOP, ArrayIndexOutOfBoundsException: 0
Reply #5 - Jan 25th, 2009, 11:30pm
 
Yeah, thanks, that makes perfect sense.  Now I get NUllPointerException however, when trying to initialize xyz[0][0] = 0;


Code:

class attractor{
// float a,b,c,d,e,f;
int points;
double bigX,bigY,bigZ;
float[] x;
float[] y;
float[] z;
float[][]xyz = {x,y,z};

attractor(int pointNb){
points = pointNb;
x = new float[points];
y = new float[points];
z = new float[points];
xyz[0][0] = 0;
xyz[1][0] = 0;
xyz[2][0] = 0;
}


void popSet(float a, float b, float c, float d, float e, float f){
int n;
for (int i=0; i<points; i++){
n = (i+1)%points;


//xyz[0][n]//
bigX = (Math.sin(a*xyz[1][i]))+ (c*(Math.cos(a*xyz[0][i])));
//xyz[1][n]//
bigY = (Math.sin(b*xyz[0][i]))+ (d*(Math.cos(b*xyz[1][i])));
//xyz[2][n]//
bigZ = (Math.sin(e*xyz[1][i]))+ (f*(Math.cos(f*xyz[1][i])));
xyz[0][n] = (float)bigX;
xyz[1][n] = (float)bigY;
xyz[2][n] = (float)bigZ;

}
}

float getX(int i){
return(xyz[0][i]);
}
float getY(int i){
return(xyz[1][i]);
}
float getZ(int i){
return(xyz[2][i]);
}

}
Re: OOP, ArrayIndexOutOfBoundsException: 0
Reply #6 - Jan 26th, 2009, 12:04am
 
It's my fault, I thought you could keep float[][]xyz = {x,y,z}; but it doesn't work.
Try:
Code:
class attractor{
// float a,b,c,d,e,f;
int points;
double bigX,bigY,bigZ;
float[] x;
float[] y;
float[] z;
float[][]xyz = {x,y,z};

attractor(int pointNb){
points = pointNb;
x = new float[points];
y = new float[points];
z = new float[points];
float[][]xyz_ = {x,y,z};
xyz = xyz_;
xyz[0][0] = 0;
xyz[1][0] = 0;
xyz[2][0] = 0;
}
Re: OOP, ArrayIndexOutOfBoundsException: 0
Reply #7 - Jan 26th, 2009, 12:12am
 
PhiLho,
 thanks, that did work, now I can move on to my other classes.  I'll post when finished.
Re: OOP, ArrayIndexOutOfBoundsException: 0
Reply #8 - Jan 26th, 2009, 12:43am
 
Okay, here's a sketch that actually gets rendered.  The idea here was to populate and array with the attractor set and then extend those points to the surface of a sphere.  In theory, I'll do this by converting the x,y,z of each point into spherical coords(rho,phi,theta).  Then, If I convert back to cartesian with a fixed value for rho, the second set of points SHOULD all live on a sphere, right?  Anyway, here's the example.  Doesn't quite look as it should.  Gotta check my math.



Code:

import processing.opengl.*;
int ang;
sph2cart s2c;
cart2sph c2s;
attractor set1;


void setup(){
size(500,500,OPENGL);
s2c = new sph2cart();
c2s = new cart2sph();
set1 = new attractor(5000);
set1.popSet(-1.4,1.6,1,0.7,-1.3,0.67);
}

void draw(){


ambientLight(255,255,255,width/3,height/3,200);
translate(width/2, height/2);
// background(0);//
rotateY(radians(ang/3));
rotateX(radians(ang/2));

ang++;


makePoints();}


void makePoints(){

background(0);
stroke(255,127,255,120);

for(int i=0;i<set1.setLength();i++){
strokeWeight(2);

float x1=set1.getX(i)*height/5; float y1=set1.getY(i)*height/5; float z1=set1.getZ(i)*height/5;
float r=c2s.r(x1,y1,z1); float p=c2s.p(x1,y1,z1); float t=c2s.t(x1,y1,z1);
float x2=s2c.x(height,p,t); float y2=s2c.y(height,p,t); float z2=s2c.x(height,p,t);

beginShape(LINES);
vertex(x1,y1,z1);
vertex(x2,y2,z2);
endShape();

}

ang++;
}

class attractor{
// float a,b,c,d,e,f;
int points;
double bigX,bigY,bigZ;
float[] x;
float[] y;
float[] z;
float[][]xyz = {x,y,z};

attractor(int pointNb){
points = pointNb;
x = new float[points];
y = new float[points];
z = new float[points];
float[][]xyz_ = {x,y,z};
xyz = xyz_;
xyz[0][0] = 0;
xyz[1][0] = 0;
xyz[2][0] = 0;
}


void popSet(float a, float b, float c, float d, float e, float f){
int n;
for (int i=0; i<points; i++){
n = (i+1)%points;


//xyz[0][n]//
bigX = (Math.sin(a*xyz[1][i]))+ (c*(Math.cos(a*xyz[0][i])));
//xyz[1][n]//
bigY = (Math.sin(b*xyz[0][i]))+ (d*(Math.cos(b*xyz[1][i])));
//xyz[2][n]//
bigZ = (Math.sin(e*xyz[1][i]))+ (f*(Math.cos(f*xyz[1][i])));
xyz[0][n] = (float)bigX;
xyz[1][n] = (float)bigY;
xyz[2][n] = (float)bigZ;

}
}
int setLength(){
return(points);
}

float getX(int i){
return(xyz[0][i]);
}
float getY(int i){
return(xyz[1][i]);
}
float getZ(int i){
return(xyz[2][i]);
}

}
class cart2sph{
float r,p,t;
cart2sph(){
}
float r(float x,float y,float z){
r = sqrt((x*x)+(y*y)+(z*z));
return r;
}

float p(float x,float y,float z){
p = atan2(z, sqrt(x*x+y*y));
return p;
}

float t(float x,float y,float z){
t = atan2(y,x);
return t;
}
}


class sph2cart{
float x,y,z;
sph2cart(){
}
float x(float r,float p,float t){
x = r*cos(p)*cos(t);
return x;
}
float y(float r,float p,float t){
y = r*cos(p)*sin(t);
return y;
}
float z(float r,float p,float t){
z = r*sin(p);
return z;
}
}

Page Index Toggle Pages: 1