Reputation: 271
I'm generally confused for the first time in awhile. I wrote this code last night in a few minutes and it worked fine. Now suddenly after expanding my list it throws an ArrayIndexOutOfbounds: 63
private final String[] itemName = new String['?'];
private final String[] itemID = new String['?'];
private void loadList() throws IOException {
final URL itemListURL = new URL("testlist.txt");
final BufferedReader buffer = new BufferedReader(
new InputStreamReader(itemListURL.openStream()));
String line;
while ((line = buffer.readLine()) != null) {
final String[] itemListArray = line.split(",");
final int i = Integer.parseInt(itemListArray[0]);
this.itemID[i] = itemListArray[0];
this.itemName[i] = itemListArray[1];
}
buffer.close();
}
Could anyone point out what I'm doing incorrectly?
Upvotes: 0
Views: 87
Reputation: 68887
private final String[] itemName = new String['?'];
private final String[] itemID = new String['?'];
Here you are creating arrays of size 63, which is the ASCII code point for a question mark ('?'
).
What you are looking for are Lists: they grow if needed.
private final List<String> itemName = new ArrayList<String>();
I think that in your case, you even don't need the itemID's, because the itemID is equal to the index of the element in the list.
Upvotes: 5