We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,007 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

java.lang.ArrayIndexOutOfBoundsException

i am just trying to populate this array with some information. i have no idea why, but i am getting:

java.lang.ArrayIndexOutOfBoundsException: 0

im probably just stupid 0_o... but, here is my code:

private boolean populated = false;

private int blockList[][] = {};

public void update(){

    if(populated == false){
        int a = 0;
        for(int i = 0; i < 200; i++){
            for(int j = 0; j < 50; j++){
                blockList[a][0] = i;
                blockList[a][1] = j;
                blockList[a][2] = 1;
                a++;
            }
        }
        populated = true;
    }

}

all input, even if not direct help, is very much appreciated.

3
Contributors
4
Replies
7 Hours
Discussion Span
9 Months Ago
Last Updated
7
Views
Question
Answered
2concussions
Newbie Poster
6 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

java.lang.ArrayIndexOutOfBoundsException: 0

That error says the array is defined with no elements. There is not an element at index 0;

You need to define the array large enough to hold all the data you want to put into it.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

A [][] array is an array of arrrays. First you need to initialise the first/outer array, then you need to initialise the inner/second arrays, although you can do both at once provided all the inner arrays are the same length egint[][] blocklist = new int[6][3]
Your code just initialised the outer array to have no elements - {} - in which case there are no inner arrays at all. If that array is zero length, then any index will be out opf range.

JamesCherrill
... trying to help
Moderator
8,512 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

thatnk you! fixed it! i understand now, too. does everything like that have to be initialised before i can use it?

2concussions
Newbie Poster
6 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Yes, only primitives have useful default values.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Question Answered as of 9 Months Ago by NormR1 and JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0666 seconds using 2.69MB