0

I'm trying to add an object called Cell to an ArrayList called Cells. When I run my code I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: 
Index: 0, Size: 0

Here's my code for my main class.

/*
This program replicates the Flipper program that I made in Processing. 
It's a light-out kind of game where the object of the game is to turn all of the cells black.
*/
package flipper;

import java.awt.Color;
import java.awt.Graphics;

import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
  *
* @author 21psuby
 */
public class Flipper {

JFrame frame;
DrawPanel drawPanel;
ArrayList<Cell> Cells = new ArrayList<>();

int screenW = 450;
int screenH = 550;

int squares = 3; //Cells on one side
int totalSquares = squares * squares; //Total cells on the screen

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    new Flipper().drawCell();

    new Flipper().run();
}

private void run() {
    frame = new JFrame();
    drawPanel = new DrawPanel();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
    frame.setVisible(true);
    frame.setSize(screenW, screenH);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
}

class DrawPanel extends JPanel {

    private static final long SerialVersionUID = 1L;

    @Override
    public void paintComponent(Graphics g) {
        for (int i = 0; i < totalSquares; i++) {
            Cell cell = Cells.get(i);
            int x = cell.getX();
            int y = cell.getY();
            int side = cell.getSide();
            int curve = cell.getCurve();
            g.drawRoundRect(x, y, side, side, curve, curve);
        }
    }
}

private void drawCell() {
    double x = 0;
    double y = 0;
    double side = screenW / squares;
    for (int i = 0; i < squares; i++) {
        for (int j = 0; j < squares; j++) {
            Cells.add(new Cell(x, y, side));
            x += side;
        }
        y += side;
    }
}

}

This is my cell class

/*
 * This program
 */
package flipper;

/**
 *
 * @author 21psuby
 */
public class Cell {

private final double x;
private final double y;
private final double side;
private final int curve = 16;

Cell(double xVal, double yVal, double sideVal) {
    x = xVal;
    y = yVal;
    side = sideVal;
}

public int getX() {
    return (int) x;
}

public int getY() {
    return (int) y;
}

public int getSide() {
    return (int) side;
}

public int getCurve() {
    return curve;
}
}

I've looked at other ArrayList questions and tried those, but I couldn't get it to work.

Thanks, Pranav

5
  • 6
    Dangerous code: for (int i = 0; i < totalSquares; i++) { Change to for (int i = 0; i < Cells.size(); i++) { Much safer Commented Feb 5, 2018 at 2:14
  • 2
    As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others. Commented Feb 5, 2018 at 2:14
  • Do not post so much code. Post a minimal reproducible example : post the bare minimum to show your issue. (Writing a minimal case is a good debugging technique. In doing so you are likely to solve your problem.) Commented Feb 5, 2018 at 5:06
  • 1
    @HovercraftFullOfEels I changed that now. I didn't realize you could do that. And I changed the names. Commented Feb 5, 2018 at 20:24
  • 1
    @c0der Next time I'll use less code Commented Feb 5, 2018 at 20:25

1 Answer 1

1

In your main function, each time you call drawCell() & run(), you instantiated a new Flipper object. They are 2 different instance of Flipper object, let's called them instance A & B.

Instance B which you called run() on, it's member variable Cells is an empty array, with size 0. So it throws IndexOutOfBoundsException when you called Cells.get(i).

You should use same object instance for this. Try replacing the below code in your main function:

public static void main(String[] args) {
    Flipper flipper = new Flipper();

    flipper.drawCell();
    flipper.run();
}

Hope this help, good luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.