2012-09-25 38 views
-1

我正在爲一家吉他商店製作一個小型j2me應用程序。 但我得到java.lang.IndexOutOfBoundsException錯誤。 我認爲可變聲明存在問題,但無法解決。 有人可以幫我一下吧。java.lang.IndexOutOfBoundsException錯誤

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
import java.util.*; 

public class guitarStore extends MIDlet implements CommandListener 
{ 
    public int guitartype=0, guitarsize=0; 

    Form frm1; 
    Form frm2; 
    Form frm3; 
    Form frm4; 
    Form frm5; 
    Form frm7; 

    TextField tfUName; 
    TextField tfPswd; 
    TextField Amount; 

    String s1, s2, s3; 
    ChoiceGroup cgAccount; 
    ChoiceGroup cgTrans; 

    Command cmdOk; //frm1 
    Command cmdExit1;//frm1 
    Command cmdLogin;//frm2 
    Command cmdBack;//frm2 
    Command cmdNextTR;//frm3 
    Command cmdBackLG;//frm3 
    Command cmdBackAC;//frm4 
    Command cmdPAmount;//frm4 
    Command cmdBackTR;//frm5 
    Command cmdNote;//frm5 
    Command cmdExit2;//page7 
    Command cmdExit4;//page7 

    private Display display; 

    private Ticker frmTicker; 



    public guitarStore() 
    { 
     try 
     { 
      frm1 = new Form("Welcome"); 
      frm2 = new Form("Member Login"); 
      frm3 = new Form("Guitar Type"); 
      frm4 = new Form("Guitar Style"); 
      frm5 = new Form("Amount Page"); 
      frm7 = new Form("Receipt"); 

      display = Display.getDisplay(this); 

      frmTicker = new Ticker ("Furtados Guitar Store "); 
      frm1.setTicker(frmTicker); 

      cmdOk=new Command("NEXT",Command.SCREEN,1);//frm1 
      cmdExit1=new Command("EXIT",Command.EXIT,0);//frm1 
      tfUName=new TextField("Enter User Name : ","",30,TextField.ANY); 
      tfPswd=new TextField("Enter Password : ","",30,TextField.PASSWORD); 
      cmdBack=new Command("BACK",Command.BACK,0);//frm2 
      cmdLogin=new Command("Login",Command.SCREEN,1);//frm2 

      cgAccount=new ChoiceGroup("Guitar Style : ",Choice.EXCLUSIVE); 
      cgAccount.append("Acoustic Guitar",null); 
      cgAccount.append("Electric Guitar",null); 
      cgAccount.append("Bass Guitar",null); 
      cmdBackLG=new Command("BACK",Command.BACK,0);//frm3 
      cmdNextTR=new Command("NEXT",Command.SCREEN,1);//frm3 

      cgTrans=new ChoiceGroup("Size Selection : ",Choice.EXCLUSIVE); 
      cgTrans.append("Small ",null); 
      cgTrans.append("Medium",null); 
      cgTrans.append("Jambo",null); 
      cmdBackAC=new Command("BACK",Command.BACK,0);//frm4 
      cmdPAmount=new Command("NEXT",Command.SCREEN,1);//frm4 

      Amount=new TextField("Total Quantity : ","",10,TextField.ANY); 
      cmdBackTR=new Command("BACK",Command.BACK,0);//frm5 
      cmdNote=new Command("Next",Command.SCREEN,1);//frm5 

      cmdExit2=new Command("BACK",Command.BACK,0);//page7 

      cmdExit4=new Command("EXIT",Command.BACK,0);//page7 

     } 
     catch(Exception e) 
     { 
      System.out.println (e); 
     } 
    } 


    public void startApp() 
    { 
     frm1.addCommand(cmdOk); 
     frm1.addCommand(cmdExit1); 

     try 
     { 
      Image im = Image.createImage("/airplane.png"); 
      frm1.append(new ImageItem(null, im, ImageItem.LAYOUT_CENTER , null)); 
      display.setCurrent(frm1); 
     } 
     catch (java.io.IOException e) 
     { 
      System.err.println("Unable to locate or read the image file"); 
     } 

     frm1.setCommandListener(this); 

     frm2.addCommand(cmdBack); 
     frm2.addCommand(cmdLogin); 
     frm2.append(tfUName); 
     frm2.append(tfPswd); 
     display=Display.getDisplay(this); 
     frm2.setCommandListener(this); 

     String msg = "Please select the Guitar type"; 
     frm3.append(msg); 
     frm3.append(cgAccount); 
     frm3.addCommand(cmdBackLG); 
     frm3.addCommand(cmdNextTR); 
     frm3.setCommandListener(this); 
     display=Display.getDisplay(this); 

     frm4.append(cgTrans); 
     frm4.addCommand(cmdBackAC); 
     frm4.addCommand(cmdPAmount); 
     frm4.setCommandListener(this); 
     display=Display.getDisplay(this); 

     frm5.addCommand(cmdBackTR); 
     frm5.addCommand(cmdNote); 
     frm5.append(Amount); 
     display=Display.getDisplay(this); 
     frm5.setCommandListener(this); 

     frm7.addCommand(cmdExit2); 
     frm7.addCommand(cmdExit4); 
     display=Display.getDisplay(this); 
     frm7.setCommandListener(this); 


    } 
    public void pauseApp(){ 

    } 

    public void destroyApp(boolean unconditional) { 
     notifyDestroyed(); 
    } 

    public void commandAction(Command c,Displayable d) 
    { 

     try 
     { 
      if(c==cmdExit1) 
      { 
       destroyApp(false); 
       notifyDestroyed(); 
      } 
      if(c==cmdOk) 
      { 
       display.setCurrent(frm2); 
      } 
      if(c==cmdLogin) 
      { 
       s2 = tfUName.getString(); 
       s3 = tfPswd.getString(); 
       if((s2.equals("admin"))&&(s3.equals("sumesh"))) 
       { 
        display.setCurrent(frm3); 
       } 
      } 
      if(c==cmdBack) 
      { 
       display.setCurrent(frm1); 
      } 

      if(c==cmdNextTR) 
      { 
       boolean choice1; 
       choice1 = cgTrans.isSelected(0); 
       boolean choice2; 
       choice2 = cgTrans.isSelected(1); 
       boolean choice3; 
       choice3 = cgTrans.isSelected(3); 

       if(choice1==true) 
       { 
        guitartype=1; 

       } 
       if(choice2==true) 
       { 
        guitartype=2; 
       } 

       if(choice3==true) 
       { 
        guitartype=3; 
       } 

       display.setCurrent(frm4); 
      } 

      if(c==cmdBackLG) 
      { 
       guitartype=0; 
       display.setCurrent(frm2); 
      } 

      if(c==cmdPAmount) 
      { 
       boolean choice4; 
       choice4 = cgTrans.isSelected(0); 
       boolean choice5; 
       choice5 = cgTrans.isSelected(1); 
       boolean choice6; 
       choice6 = cgTrans.isSelected(3); 

       if(choice4==true) 
       { 
        guitarsize=3000; 

       } 
       if(choice5==true) 
       { 
        guitarsize=5000; 
       } 

       if(choice6==true) 
       { 
        guitarsize=7000; 
       } 

       display.setCurrent(frm5); 
      } 

      if(c==cmdBackLG) 
      { 
       guitarsize=0; 
       display.setCurrent(frm3); 
      } 


      if(c==cmdNote) 
      { 
       s1 = Amount.getString(); 
       int s4 = Integer.parseInt(s1); 
       if(s4>10) 
       { 
        Alert bal1 = new Alert("Transaction not allowed","Not more than 10 guitars can be purchased at a time", null, AlertType.INFO); 
        bal1.setTimeout(5000); 
        display.setCurrent(bal1,frm7); 
       } 
       else 
       { 
        int prize,totalprize; 
        prize=s4*(guitartype*guitarsize); 
        totalprize=prize; 

        frm7.append("Total cost of purchase is Rs: " + prize + ""); 
        display.setCurrent(frm7); 
       } 
      } 

      if(c==cmdBackTR) 
      { 
       display.setCurrent(frm4); 
      } 

      if(c==cmdExit2) 
      { 
       display.setCurrent(frm4); 
      } 

      if(c==cmdExit4) 
      { 
       destroyApp(false); 
       notifyDestroyed(); 
      } 
     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
     } 
    } 
} 
+1

請發佈堆棧跟蹤! –

+3

@duffymo它不是強制性的,每個人都知道如何編寫一個任何人都可以理解的代碼。如果你不想付出自己的努力,那麼不要......但至少不要批評某人缺乏經驗在..當然,我想你也會從你的經驗中學到.. –

+0

@Sumesh我甚至不會問你是否調試過它。這是不值得的。只需從頭開始重寫它,但這次做得正確(至少給變量一些重要的名字)。 –

回答

3

您正試圖籤第4選擇指數(2處):

choice3 = cgTrans.isSelected(3); 

但只填充了ChoiceGroup部件只有3項。更好使用:

choice3 = cgTrans.isSelected(2); 
+0

@ Reimeus-非常感謝您的幫助 –

相關問題