3 Replies - 3379 Views - Last Post: 12 October 2011 - 02:36 AM Rate Topic: -----

#1 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Out of Array Bounds exception, when not?

Posted 12 October 2011 - 02:18 AM

I am getting the following exception when iterating through my array. I have 10 elements (array[9]), but I am only able to cycle up to 9 of them before getting this error. Am I missing something really obvious here? I can't complete the painting of the map without it, because the right and bottom sides are in the last element.

I'm really confused here. All help is appreciated.

java.lang.ArrayIndexOutOfBoundsException: 9
	at GameScreen.<init>(GameScreen.java:24)
	at Main.init(Main.java:16)
	at sun.applet.AppletPanel.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)



public class GameScreen extends Screen implements Runnable {

	public Tile[][] tiles = new Tile[9][9];

// [9][9] = 10 elements each dimension, right?
// Level1 here is 10 elements each dimension, [0-9][0-9]

	int[][] level1 = new int[][] {{0,0,0,0,0,0,0,0,0,0},
		{0,0,0,0,0,0,0,0,0,0},
		{1,1,0,0,0,0,0,0,0,0},
		{0,1,0,0,0,0,0,0,0,0},
		{0,1,0,0,0,0,0,0,0,0},
		{0,1,1,1,1,1,1,0,0,0},
		{0,0,0,0,0,0,1,0,0,0},
		{0,0,1,1,1,1,1,0,0,0},
		{0,0,1,0,0,0,0,0,0,0},
		{0,0,1,1,1,1,1,0,0,0}};
	
	public GameScreen() {

// <10 is the same as going up to 9, <=9 also does not work.
		for(int i = 0; i < 10; i++) {
			for(int j = 0; j < 10; j++) {
				if (level1[i][j] == 0) {
					this.tiles[i][j] = new Grass(); //line 24
					this.tiles[i][j].xLoc = i * 45;
					this.tiles[i][j].yLoc = j * 45;
				} 
				else if (level1[i][j] == 1) {
					this.tiles[i][j] = new Dirt();
					this.tiles[i][j].xLoc = i * 45;
					this.tiles[i][j].yLoc = j * 45;
				}
			}
		}
	}

	public void paintComponent(Graphics g) {
		g.setColor(Color.orange);
		g.fillRect(0, 0, getWidth(), getHeight());
		for(int i = 0; i < 10; i++) {
			for(int j = 0; j < 10; j++) {
				if (this.tiles[i][j] != null) {
					this.tiles[i][j].paint(g);
				}
			}
		}
	}

	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			repaint();
			try {
				Thread.sleep (50);
			} catch (Exception exc) { 
			
			};
		}
	}
}

This post has been edited by Mylo: 12 October 2011 - 02:23 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Out of Array Bounds exception, when not?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Out of Array Bounds exception, when not?

Posted 12 October 2011 - 02:26 AM

You have to say: public Tile[][] tiles = new Tile[10][10]; that means 10 elements 0-9 because the index of array ends at length -1.
So in your loop you go up to 9 which will give array out of bounds because tiles has size 9 so index 0 -8
Was This Post Helpful? 2
  • +
  • -

#3 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Out of Array Bounds exception, when not?

Posted 12 October 2011 - 02:32 AM

Oh wow, how did I miss that, I could of sworn I tried every number from 8-11 in both the array and the loops. Ah well, my mistake. Thank you very much :)
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Out of Array Bounds exception, when not?

Posted 12 October 2011 - 02:36 AM

Glad we could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1