问题描述
java.lang.IndexOutOfBoundsExceptionatjavax.microedition.lcdui.ChoiceGroup.checkIndex(+19)atjavax.microedition.lcdui.ChoiceGroup.getString(+11)atInputForm.GetFeeCode(+15)atInputForm.<init>(+203)atMain_Form.commandAction(+100)atjavax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)atjavax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)atcom.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)atcom.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)atcom.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.handleVmEvent(+194)atcom.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+51)程序:publicclassInputFormextendsFormimplementsCommandListener,ItemStateListener{privateDateFielddatefield;privateChoiceGroupfeenamecg;privateChoiceGroupsubfeenamecg;privateTextFieldfee;privateTextFieldmemo;privateStringcode;privateStringsubcode;privateStringmemovalue;privateintfeevalue;privatelongdate;privateCommandsaveCommand;privateCommandbackCommand;privateDisplaydis;privateStringsubfeename;privateStringfeename;publicInputForm(Displaydisplay){super("每日费用录入");code=newString();subcode=newString();memovalue=newString();subfeename=newString();feename=newString();dis=display;datefield=newDateField("选择日期:",1);append(datefield);Datedate=newDate();datefield.setDate(date);GetDate();feenamecg=newChoiceGroup("选择费用类别:",1);for(inti=0;i<Account.feenamevector.size();i++){Fee_Typefeetype=(Fee_Type)Account.feenamevector.elementAt(i);feenamecg.append(feetype.name,null);}append(feenamecg);subfeenamecg=newChoiceGroup("选择费用明细:",1);append(subfeenamecg);GetFeeCode();GetSubFeeCode();fee=newTextField("费用(元):",null,4,2);append(fee);memo=newTextField("备注:",null,8,0);append(memo);backCommand=newCommand("返回",Command.BACK,2);addCommand(backCommand);saveCommand=newCommand("保存",Command.SCREEN,3);addCommand(saveCommand);setCommandListener(this);setItemStateListener(this);}publicvoidcommandAction(Commandcommand,Displayabledisplayable){if(command==backCommand){Main_Formmainform=newMain_Form(dis);dis.setCurrent(mainform);return;}if(command==saveCommand){Stringtemp=newString(fee.getString());if(fee.getString().length()==0){try{Alertalert=newAlert("警告","请输入费用",Account.alert,AlertType.ALARM);alert.setTimeout(3000);dis.setCurrent(alert);}catch(Exceptionexception){}return;}feevalue=Integer.parseInt(temp);if(feevalue==0){Alertalert=newAlert("警告","请输入费用",Account.alert,AlertType.ALARM);alert.setTimeout(3000);dis.setCurrent(alert);fee.setString("");feevalue=0;}else{encode();Alertalert=newAlert("提示","保存完毕",Account.alert,AlertType.ALARM);alert.setTimeout(3000);dis.setCurrent(alert);fee.setString("");feevalue=0;}}}publicvoiditemStateChanged(Itemitem){if(item==feenamecg)GetFeeCode();if(item==subfeenamecg)GetSubFeeCode();if(item==datefield)GetDate();if(item!=fee);if(item==memo)memovalue=memo.getString();}publicvoidencode(){byteresult[]=null;try{ByteArrayOutputStreambos=newByteArrayOutputStream();DataOutputStreamdos=newDataOutputStream(bos);dos.writeLong(date);dos.writeUTF(code);dos.writeUTF(subcode);dos.writeInt(feevalue);dos.writeUTF(memovalue);result=bos.toByteArray();dos.close();bos.close();}catch(Exceptionex){return;}WriteDetail(result);}publicvoidWriteDetail(byteresult[]){Stringfilename=newString("fee_detail");RecordStorefeedetaildb;try{feedetaildb=RecordStore.openRecordStore(filename,true);}catch(Exceptionex){return;}try{feedetaildb.addRecord(result,0,result.length);}catch(Exceptionex){return;}try{feedetaildb.closeRecordStore();}catch(Exceptionex){return;}}publicvoidGetDate(){date=datefield.getDate().getTime();}publicvoidGetSubFeeCode(){subfeename=subfeenamecg.getString(subfeenamecg.getSelectedIndex());inth=0;do{if(h>=Account.subfeenamevector.size())break;Sub_Fee_Typesubfeetype=(Sub_Fee_Type)Account.subfeenamevector.elementAt(h);if(code.equals(subfeetype.code)&&subfeename.equals(subfeetype.name)){subcode=subfeetype.subcode;break;}h++;}while(true);}publicvoidGetFeeCode(){feename=feenamecg.getString(feenamecg.getSelectedIndex());inti=0;do{if(i>=Account.feenamevector.size())break;Fee_Typefeetype=(Fee_Type)Account.feenamevector.elementAt(i);if(feename.equals(feetype.name)){code=feetype.code;break;}i++;}while(true);intm=subfeenamecg.size();for(intd=0;d<m;d++)subfeenamecg.delete(0);intcount=0;for(inth=0;h<Account.subfeenamevector.size();h++){Sub_Fee_Typesubfeetype=(Sub_Fee_Type)Account.subfeenamevector.elementAt(h);if(code.equals(subfeetype.code)){subfeenamecg.insert(count,subfeetype.name,null);count++;}}}}
解决方案
解决方案二:
publicvoidGetFeeCode(){feename=feenamecg.getString(feenamecg.getSelectedIndex());GetFeeCode()方法在类构造时被调用,而由于此时还未选择,getSelectedIndex应该返回的是-1
解决方案三:
java.lang.IndexOutOfBoundsExceptionatjavax.microedition.lcdui.ChoiceGroup.checkIndex(+19)atjavax.microedition.lcdui.ChoiceGroup.getString(+11)atInputForm.GetFeeCode(+15)看异常就知道了啊。。。。数组下标越界,位置:方法GetFeeCode里ChoiceGroup里进行的选中下标操作。。。。
解决方案四:
顶楼上
解决方案五:
引用1楼kf156的回复:
publicvoidGetFeeCode(){feename=feenamecg.getString(feenamecg.getSelectedIndex());GetFeeCode()方法在类构造时被调用,而由于此时还未选择,getSelectedIndex应该返回的是-1
那要怎么改啊。