Loading...
Logo
Processing Forum
Dear forum


So i got this little code going on here and everything works right. but now i do want to put an automation in generating new objects, at where the program starts to fail, giving me the line: ArrayIndexOutOfBoundsExeptions: 9

The program also highlights this line:

hallo[index++] = new Hello(geschwindigkeit, wahrfalsch);
Copy code



Hello[] hallo;

boolean wahrfalsch;
float geschwindigkeit;

int anzahl = 9;
int index;


void setup()
{
  size(500, 500);
  frameRate(90);
  background(255);
  randomSeed(40);
  hallo = new Hello[anzahl];
  for(int b = 0; b <= anzahl; b++)
  {
    geschwindigkeit = random(8);
    if (random(10) < 5) wahrfalsch = false;
    hallo[index++] = new Hello(geschwindigkeit, wahrfalsch);
  }

}

void draw()
{
  for(int b = 1; b <= anzahl; b++)
  {
    hallo[b].farbenundpos();
    hallo[b].fade();
    hallo[b].zeigen();
  }
 
}






class Hello
{
  float r, g, b;
  long l;
  float k;
  float x, y, x2, y2;
  boolean toll = false;
  boolean aufab;
  
  Hello(float fadegesch, boolean updown)
  {
    k = fadegesch;
    aufab = updown;
    
  }
 
  
  void farbenundpos()
  {
    if (toll == false){
    r = random(255);
    g = random(255);
    b = random(255);
    x = random(500);
    y = random(500);
    x2 = random(500);
    y2 = random(500);
    }
    toll = true;
  }
  
  
  void fade()
  {
    l++;
    if(l % 2 == 0) background(255);
    if(aufab == false){
    if(l % k == 0)
    {
      r--;
      g--;
      b--;
    }
    }
    
    else if ( aufab == true)
    {
      if(l % k == 0){
      r++;
      g++;
      b++;
      }
    }
      
    
    if ((r + g + b) < 1) toll = false;
    if ((r + g + b) > (3*255)) toll = false;
  }
  
  
  void zeigen()
  {
    stroke(r, g, b);
    line(x, y, x2, y2);
  } 
}
Copy code
  1.  

Has anybody ever encounterd this problem?

thanks in advance

Sincerely 

bluebubble

Replies(7)

for(int b = 0; b <= anzahl; b++)
In Java, arrays holds n values, so indexes are going from 0 up to n-1. Change the red code with b < anzahl

It's working. thanks
Next thing i'm trying to do is to add a new object by pressing the left mouse button. the code is the same. I just added following lines:

void draw()
{
  for(int u = 1; u < anzahl; u++)
  {
    hallo[u].farbenundpos();
    hallo[u].fade();
    hallo[u].zeigen();
  }
  if(mousePressed && mouseButton == LEFT && aron == false)
  {
    hallo = new Hello[anzahl++];
    if(b % 2 == 0) wahrfalsch = true;
    else wahrfalsch = false;
    hallo[b++] = new Hello(3, wahrfalsch);
    aron = true;
  }
    
 
}

void mouseReleased()
{
  aron = false;
}

The line highlighted red is the one also highlighted by the program.


any ideas?
You mean you have an error on this line? What kind of error? Where b comes from?

Side note: you can write:
wahrfalsch = b % 2 == 0;
Booleans are often underused by beginners...

Oh, and if you have a variable number of elements, you better use an array list. With  hallo = new Hello[anzahl++]; you loose the previous values in the array (and perhaps you wanted ++anzahl instead, if you want to increment before resizing the array).
So I wrote an other Sketch to try to experiment at adding objects, but it's not working. It's working until i try to add an object:

Copy code
  1. Auto[] auto;

  2. boolean aron = false;
  3. int i;
  4. int counter;

  5. int anzahl = 6;

  6. void setup()
  7. {
  8.   size(500, 500);
  9.   frameRate(60);
  10.   auto = new Auto[anzahl+ 100];
  11.   for(int i = 0; i < anzahl; i++)
  12.   {
  13.     auto[i] = new Auto();
  14.     counter++;
  15.   }
  16. }



  17. void draw()
  18. {
  19.   background(255);
  20.   for(int count = 0; count < counter; count++)
  21.   {
  22.   auto[count].bewegen();
  23.   auto[count].zeigen();
  24.   }
  25.   if(keyPressed && key == 'k' && aron == false)
  26.   {
  27.     auto[++counter] = new Auto();
  28.     aron = true;
  29.   }
  30. }
As I press "k" I get following line:

NullPointerException

Highlighted is the line in red.

I put the auto = new Auto[anzahl+ 100]; to plus 100, so actually nothig should go wrong.

any ideas?

thanks in advance
Well, here you need to do auto[counter++] = new Auto();
When you end the loop in setup, counter is already one unit beyond the max array size. The usage here is different from the first case I pointed to...
It'sa working. Thanks wizard!!