ArrayIndexOutOfBoundsException

edited October 2017 in Questions about Code

So I have this silly error, I've already looked it up on this site but all I will not work, so I hope you can find my mistake. It's a programm about a spaceship and this little spaceship should shoot lasers every time you hit the mouse. The lasers are not done yet, I just want to figure out how to add objects in an array every time I click the mouse. Code :

[see below for code]

If there are questions about the code feel free to ask, thank you very much!

Answers

  • edit post, highlight code, press ctrl-o to format

    what is i here?

    lasershots[i].Laser();
    
  • edited October 2017

    [Unformatted code deleted]

    i is the variable for the array length

  • edit post, highlight code, press ctrl-o to format

    What value does it have?

  • in setup() there is the for loop in which I gave it the value 0, and as long as it is less than 10, it should go up by one (i++). so It counts the index array from 0, but it wont work

  • We can't read your code until you format it so you need to do that before we can comment further.

  • it wont work

    And this is the laziest and most worthless of bug reports

  • edited October 2017
    /** VARIABLES */
    //PLAYBUTTON
    boolean playButtonFSCheck = true;
    boolean playButtonFSMouseActive = true;
    
    int i;
    
    /** OBJECTS */
    //object + variable
    Ship ship;
    LaserShots[] lasershots = new LaserShots[10];
    /** SETUP */
    void setup() {
      //size(600,1000);
      fullScreen();
      ship = new Ship();
      for (i = 0; i < lasershots.length; i++) {
        lasershots[i] = new LaserShots();
      }
    }
    
    /**--DRAW*/
    void draw() {
      background(51);
    
      //starttext
      fill(255);
      textSize(150);
      if(playButtonFSCheck) {
      text("SHOOTER", width/2-325, height/4);
      }
    
    
    
      //playbutton
      fill(102, 255, 51);
      stroke(0);
      strokeWeight(5);
      if(playButtonFSCheck) {
        triangle(width/2-150,height/2-200, width/2+250, height/2, width/2-150, height/2+200);
      }
    
      //SHIP DISPLAY
      ship.display();
      //SHIP MOVE
      ship.move();
    
      lasershots[i].Laser();
    }
    /** MOUSEPRESSED */
    void mousePressed() {
      //Playbutton
      if(playButtonFSMouseActive) {
        if(mouseX > width/2-160 && mouseX < width/2+260 && mouseY > height/2-210 && mouseY < height/2+210) {
          playButtonFSCheck = !playButtonFSCheck; 
          playButtonFSMouseActive = false;
          ship.triangleCheck = true;
        }
      }
    
      for(i=0; i<lasershots.length; i++) {
        lasershots[i].Laser();
      }
    }
    
    /** CLASSES */
    class LaserShots {
      float x = random(20,width-20);
      float y = random(20,height/2);
    
      void Laser() {
        noFill();
        stroke(255,0,200);
        strokeWeight(5);
        ellipse(x,y,20,20);
      }
    }
    
  • Answer ✓

    If you click the cog on the first post you can edit that, rather than reposting the same error again and again.

    Highlight the code, press Ctrl-o, that's all it needs.

  • ok, cool. now we have line numbers so i can say things like:

    line 48, what is the value of i?

  • no clue, I don't know how I cand add a new Object to the array every time I click the mouse, I'm a bit helpless with that topic

  • no clue

    try adding line 47

    println("i: " + i);
    

    (your code is missing the Ship class btw, we can't run it)

  • edited October 2017
    int[] values = new int[10];
    
    int i;
    for (i = 0 ; i < values.length ; i++) {
      values[i] = i;
      println("Values[" + i + "]: " + values[i]);
    }
    
    println("I is now: " + i);
    println(values[i]);
    

    this is basically your current problem. the variable you use as a lopo counter is outside of the bounds when you try and use it after the loop

    it is more traditional to define the index counter inside the for loop for (int i = 0...), limiting its scope to the loop.

    line 48 just looks a bit extraneous the way it is. is it just an error? or does it need a loop around it?

    your overall problem, being able to add things to your group, is better done using an ArrayList which, unlike arrays, are designed to grow and shrink when required.

    https://processing.org/reference/ArrayList.html

Sign In or Register to comment.