• Advertisement
Sign in to follow this  

[java] java.lang.ArrayIndexOutOfBoundsException Error

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anybody help me to resolve this problem? I've had some problems with my English-Vietnamese Dictionary for Cellphone but it does not work properly. I got an error of java.lang.ArrayIndexOutOfBoundsException and here comes the detail of my error: java.lang.ArrayIndexOutOfBoundsException at Dictionary.getSelection(+285) at Dictionary.commandAction(+102) at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282) at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10) at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68) at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47) at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(+186) at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+57) Below is my code that I though the error has happened: private void buildIndex(){ int size; try { InputStream in = getClass().getResourceAsStream("/dictionary.idx"); size = in.read(buf); in.close(); } catch (IOException x) { throw new Error("L\u1ED7i \u0111\u1ECDc t\u1EC7p \nFailed to read index"); } Vector volumesList = new Vector(); for (int i = 0; i < size; i++) { int j; Volume v = new Volume(); //Here is the method to read the database. Below is the original one for (j = i; buf[j] != '@' /*origin is ':'*/; j++); v.first = new String(buf, i, j-i); for (i = ++j; buf[i] !='\n' /*origin is '\n'*/; i++); v.last = new String(buf, j, i-j); volumesList.addElement(v); } nVolumes = volumesList.size(); volumes = new Volume[nVolumes]; prevVolumeIndex = -1; for (int i = 0; i < nVolumes; i++) { volumes[i] = (Volume)volumesList.elementAt(i); } } //This function is to get word and compare with dictionary public Vector getSelection(String prefix) { if (nVolumes == 0) { buildIndex(); } Vector selection = new Vector(); int i; int n = nVolumes; Volume[] vs = volumes; boolean found = false; try { for (i = 0; i < n && vs[i].last.compareTo(prefix) < 0; i++); while (i < n) { int size; if (prevVolumeIndex != i) { /*Here is original one */ InputStream in = getClass().getResourceAsStream("volume." + (i+1)); size = in.read(buf); prevVolumeIndex = i; prevVolumeSize = size; in.close(); } else { size = prevVolumeSize; } String prevWord = vs[i].first; for (int j = 0; j < size; j++) { int start = j; while (buf[j] != SEPARATOR) { j += 1; } // String word = (buf[start] <= '9') // ? prevWord.substring(0, buf[start]-'0') + new String(buf, start+1, j-start-1) // : new String(buf, start, j-start); String word = (buf[start] > 57 ? new String(buf, start, j-start) : prevWord.substring(0, buf[start] - 48) + new String(buf, start+1, j-start-1)); start = ++j; while (buf[j] != '\n') { j += 1; } if (word.compareTo(prefix) >= 0) { //>= if (word.startsWith(prefix)) { found = true; } else if (found) { return selection; } char[] chars = new char[j-start]; for (int k = start; k < j; k++) { byte b = buf[k]; //chars[k-start] = (char)(b < 0 ? b == -88 ? 0x401 : b == -72 ? 0x451 : //(0x400 | ((b & 0x7f) - 0x30)) : b); /*This is new one and it works for Vietnamese *It is different way to read data from dictionary Vi-En */ chars[k-start] = (char)(b >= 0 ? b : b != -88 ? b != -72 ? 0x400 | (b & 0x7f) - 48 : 1105 : 1025); } selection.addElement(new Translation(word, new String(chars))); if (selection.size() == SELECTION_LIMIT) { return selection; } } prevWord = word; } i += 1; } } catch (IOException x){ x.printStackTrace(); } return selection; } I hope that someone who will help me to resolve this problem.

Share this post


Link to post
Share on other sites
Advertisement
Hi,

I have tried my best to debug your code, however the error is dependant on your volume files data. I was unable to test with complete code coverage and it was very difficult for me to even try and understand any of your code.

Every other line in your code seems to be prone to ArrayIndexOutOfBoundsExceptions. If you posted links to your volume and dictionary.idx files then perhaps I would be able to debug it further but only if I was able to reproduce your error.

The dictionary translation worked fine for a couple of example that I setup but then all lines did not get executed.

Questions?
Just one question why aren't you using String.split( String regex ) method. Did you know that you can split a String into an array of strings? Oh wait yet another question what is your file format and how is the Dictionary supposed to work?

Suggestions
If I were you I would refactor your code using Strings and not trying to do all that byte and char manipulation it is not needed and definitely not cohesive. You could probably make this program work a lot better if you used something like a HashMap for your word associations.

Just one last thing,
What does this have to do with Game Development?

Share this post


Link to post
Share on other sites
Quote:
Original post by 5MinuteGaming
Hi,

I have tried my best to debug your code, however the error is dependant on your volume files data. I was unable to test with complete code coverage and it was very difficult for me to even try and understand any of your code.

Every other line in your code seems to be prone to ArrayIndexOutOfBoundsExceptions. If you posted links to your volume and dictionary.idx files then perhaps I would be able to debug it further but only if I was able to reproduce your error.

The dictionary translation worked fine for a couple of example that I setup but then all lines did not get executed.

Questions?
Just one question why aren't you using String.split( String regex ) method. Did you know that you can split a String into an array of strings? Oh wait yet another question what is your file format and how is the Dictionary supposed to work?

Suggestions
If I were you I would refactor your code using Strings and not trying to do all that byte and char manipulation it is not needed and definitely not cohesive. You could probably make this program work a lot better if you used something like a HashMap for your word associations.

Just one last thing,
What does this have to do with Game Development?


Share this post


Link to post
Share on other sites
First of all I would like to say thank you for your help and I would appreciate your help. Here come my link of source code:

http://www.esnips.com/doc/5f0d0137-d970-4775-8579-cf160ba23e3e/moVietdict

Thank you,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement