-1

I am making a small j2me application for a guitar store . but i am getting java.lang.IndexOutOfBoundsException error . i think there is a issue with the varaible declaration, but not able to solve. can someone please help me with it.

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);
        }  
    }
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 3
    @duffymo Its not compulsory that everyone knows how to write a code that anyone can understand.. If you don't want to give your effort, then don't.. but at least don't criticize someone for what he is inexperienced in.. Of course, I think you also would have learnt from your experience.. – Rohit Jain Sep 25 '12 at 11:44
  • @Sumesh I won't even ask if you've debugged it. It's just not worth it. Just rewrite it from scratch, but do it properly this time (at least give some significant names to variables). – Mihai Todor Sep 25 '12 at 11:48
  • 1
    It's good to know that asking your IDE to reformat your code is a good thing to do. If you weren't so thin skinned you could learn something from this comment. Maybe you'll know better when your rep climbs above 377. – duffymo Sep 25 '12 at 11:48
  • @duffymo Hello duffymo.. I have edited your code tags, to make it look ( a little cleaner). Not as cleaner as required though.. But here is what I want you to know.. In Programming world, if you have any method that takes a person more than 20 seconds to understand, then probably thats bad method.. You need to refactor your code to divide your task into separate chunks.. Proceed with smaller task, and then you can gradually merge what you have done to get complete program.. – Rohit Jain Sep 25 '12 at 11:51
  • And the simplest way to divide your task is to make a method for each part.. Yes, each of your method should do only one task, not more than that.. Like a method dedicated for User Input, one for Display, one for Calculation, and so on.. Also, consider making different classes depending upon relation between various area in your Problem statement.. – Rohit Jain Sep 25 '12 at 11:54
  • @RohitJain, If you want to maintain good quality, you must to be harsh from the beginning. – Damian Leszczyński - Vash Sep 25 '12 at 11:55
  • @Vash.. Being harsh on him won't help.. Eventually he'll leave the forum and ask someone else.. Probably not getting his problem solved at the end of the day.. So it would be better to show him path rather than shouting at him for not being able to find the path.. A person will only learn by generating interest that is a gradual process, not an instant one.. – Rohit Jain Sep 25 '12 at 12:00
  • Nobody's shouting except you, Rohit Jain. I don't see how you've been nominated as a monitor after only eight days here. Telling him that format and style matters is part of showing the path. Maybe you could actually try and provide an answer yourself instead of acting like a Greek chorus in comments. Sumesh, you still haven't posted a stack trace. – duffymo Sep 25 '12 at 13:07

1 Answers1

3

You are attempting to check the 4th selected index (in 2 places):

choice3 = cgTrans.isSelected(3);

yet have only populated your ChoiceGroup widget with only 3 items. Better to use:

choice3 = cgTrans.isSelected(2);
Reimeus
  • 158,255
  • 15
  • 216
  • 276