2012-09-25 5 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

당신은 (2 곳) 4 선택된 인덱스를 확인하려고 :

choice3 = cgTrans.isSelected(3); 

아직 단지에만 3 항목으로 ChoiceGroup 위젯을 채워왔다. 더 나은 사용 :

choice3 = cgTrans.isSelected(2); 
+0

@ Reimeus- 도움 주셔서 대단히 감사합니다. –

관련 문제