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 & HelpPrograms › Errors about Java
Page Index Toggle Pages: 1
Errors about Java (Read 796 times)
Errors about Java
Sep 16th, 2009, 7:21am
 
Hi, I got some errors when I try to write a 2d array in processing 1.0.7:

java.lang.ArrayIndexOutOfBoundsException: 4
     at generater.fourDots(generater.java:75)
     at generater.setup(generater.java:33)
     at processing.core.PApplet.handleDraw(PApplet.java:1402)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:619)

Here's the code:

--------------------------------

int [][] points;

void setup()
{
 size(120,120);
 rect(20,20,80,80);
 for(int i=20;i<=80;i=i+20){
   line(20,i,100,i);
   line(i,100,i,20);
 }
 for(int x=20; x<=100; x=x+20){
   for(int y=20; y<=100; y=y+20){
     ellipse(x, y, 10, 10);
   }
 }
initial();
while (fourDots()!=true){
 randomPoint();
}
}


void initial (){
 for(int x=0; x<5; x++){
   for(int y=0; y<5; y++){
    points [x][y] =0;
   }
 }
}

void randomPoint(){
 int x = int (random(0,5));
 int y = int (random(0,5));
 print(x);
 print(y);
 if( points[x][y] ==0){
   points[x][y] =1;
   fill(0);
   ellipse(20*x,20*y,10,10);  
 }
}

boolean fourDots(){
 boolean value = false;
 int sum=0;
   for(int x=0; x<5; x++){
     for(int y=0; y<5; y++){
       sum=sum+points[x][y];
     }
   }
   if(sum>=4) {
    value= true;}
  return value;
}


Does anyone know what does that error meaning? Thanks~
Re: Errors about Java
Reply #1 - Sep 16th, 2009, 7:57am
 
int[][] points;

this doesn't initialise the array so you can't just use it immediately

int[][] points = new int[5][5];

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
Re: Errors about Java
Reply #2 - Sep 18th, 2009, 5:26am
 
Thanks a lot~
Page Index Toggle Pages: 1