1 Replies - 222 Views - Last Post: 04 February 2018 - 06:36 PM Rate Topic: -----

#1 clsproduct  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-February 18

Getting an error I don't understand

Posted 04 February 2018 - 05:39 PM

ERROR: Caused by: Java.lang.ArraryIndexOutOfBoundsException: 15

I'm having an error pop up on this line:
 if (token3[(6 + i)] != null) { 


public boolean loadItemList(String FileName) {
		String line = "";
		String token = "";
		String token2 = "";
		String token2_2 = "";
		String[] token3 = new String[10];
		boolean EndOfFile = false;
		int ReadMode = 0;
		BufferedReader characterfile = null;
		try {
			characterfile = new BufferedReader(new FileReader("./Data/cfg/"+FileName));
		} catch(FileNotFoundException fileex) {
			Misc.println(FileName+": file not found.");
			return false;
		}
		try {
			line = characterfile.readLine();
		} catch(IOException ioexception) {
			Misc.println(FileName+": error loading file.");
			return false;
		}
		while(EndOfFile == false && line != null) {
			line = line.trim();
			int spot = line.indexOf("=");
			if (spot > -1) {
				token = line.substring(0, spot);
				token = token.trim();
				token2 = line.substring(spot + 1);
				token2 = token2.trim();
				token2_2 = token2.replaceAll("\t\t", "\t");
				token2_2 = token2_2.replaceAll("\t\t", "\t");
				token2_2 = token2_2.replaceAll("\t\t", "\t");
				token2_2 = token2_2.replaceAll("\t\t", "\t");
				token2_2 = token2_2.replaceAll("\t\t", "\t");
				token3 = token2_2.split("\t");
				if (token.equals("item")) {
					int[] Bonuses = new int[12];
					for (int i = 0; i < 12; i++) {
						if (token3[(6 + i)] != null) { //<-------HERE
							Bonuses[i] = Integer.parseInt(token3[(6 + i)]);
						} else {
							break;
						}
					}
					newItemList(Integer.parseInt(token3[0]), token3[1].replaceAll("_", " "), token3[2].replaceAll("_", " "), Double.parseDouble(token3[4]), Double.parseDouble(token3[4]), Double.parseDouble(token3[6]), Bonuses);
				}
			} else {
				if (line.equals("[ENDOFITEMLIST]")) {
					try { characterfile.close(); } catch(IOException ioexception) { }
					return true;
				}
			}
			try {
				line = characterfile.readLine();
			} catch(IOException ioexception1) { EndOfFile = true; }
		}
		try { characterfile.close(); } catch(IOException ioexception) { }
		return false;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Getting an error I don't understand

#2 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Getting an error I don't understand

Posted 04 February 2018 - 06:36 PM

if (token3[(6 + i)] != null) 


This implies that 6+i is beyond the end of the array. Let's see how that could happen

i is counting from 0 to 12. That means that we would hope that token3 would have a length of at least 19, to accomodate token3[6 + 12]. (remember, arrays are zero-indexed)

However, when we look for the declaration we find that
String[] token3 = new String[10]


which would explain why we're out of bounds.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1