Original Post
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 !='\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 = (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.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.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.