Index Out Of Bounds Exception Error !

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1786 Views - Last Post: 20 July 2011 - 10:54 AM Rate Topic: -----

#1 Hello World 123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-July 11

Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:18 AM

I keep getting an out of bounds exception error can anyone see why ? I've been staring at this for hours and no luck ?

public PricePlans cheapPlan()
    {
        int i = 0;
        int small = Integer.MAX_VALUE;
        PricePlans pp;
        PricePlans a;
        int b;


        {
            //For each item in Array  List PricePlans iterate onece

            for(i = 0; i < pricePlans.size(); i ++);  
            {
                //get item out of priceplan list and store it in varaible a
                a = pricePlans.get(i);
                //call cost method on varible a and store in varaible b
                b = a.cost(usage);
                //if b is smaller than small  store b in small and store a in pp.
                if(b < small)
                small = b;
                pp  = a;
                
                //print out small and pp.
                System.out.println(small);
                System.out.println(pp);
                

            }

        }
            return pp;
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Index Out Of Bounds Exception Error !

#2 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:21 AM

Other than the fact I see unnecessary curly braces, the only array that exists would be the pricePlans ArrayList so I need to see where it's defined and/or initialized.
Was This Post Helpful? 0
  • +
  • -

#3 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:22 AM

We need to see where the variable pricePlans is instantiated (and a declaration wouldn't hurt, where the hell is it coming from?).

I'm also curious about how that for loop does anything since you put a semicolon at the end of it.

This post has been edited by xclite: 20 July 2011 - 09:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:39 AM

 if(b < small)
	                small = b;
	                pp  = a;



This won't fix your problem but you need curly braces around your two statements if you want them both to execute only when b < small.

This post has been edited by ipushmycar: 20 July 2011 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#5 Hello World 123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-July 11

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:46 AM

Hi sorry about not giving you more detail the semicolon was silly

public class PricePlansDatabase 
{
    private ArrayList<PricePlans> pricePlans;
    private Usage usage;

    public PricePlansDatabase()
    {
        pricePlans = new ArrayList<PricePlans>();
    }

    public void addUsage()
    {
        usage = new Usage(2,2);
    }

    public void addPP()
    {
        pricePlans.add( new PayMonthly("Vodafone",500));
        pricePlans.add( new PayMonthly("02",1));
        pricePlans.add( new PayMonthly("3", 0));
        pricePlans.add( new PayMonthly("Orange",3));

        pricePlans.add( new PayAsYouGo("Orange",2,2));
        pricePlans.add( new PayAsYouGo("02",3,3));
        pricePlans.add( new PayAsYouGo("Vodafoe",4,4));
        pricePlans.add( new PayAsYouGo("3",4,4));
    }

Was This Post Helpful? 0
  • +
  • -

#6 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:50 AM

where do you call addPP() ?
Was This Post Helpful? 0
  • +
  • -

#7 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:51 AM

What line is the exception thrown on? This one?
a = pricePlans.get(i);


Was This Post Helpful? 0
  • +
  • -

#8 Hello World 123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-July 11

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:57 AM

umm I didn't throw an expeption's I don't really know what it is so I avoid doing it
Was This Post Helpful? 0
  • +
  • -

#9 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:59 AM

No. The exception tells you what line it occurred on- when does the error message say it happened?
Was This Post Helpful? 0
  • +
  • -

#10 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 09:59 AM

What he meant was on what line was the out of bounds error
Was This Post Helpful? 0
  • +
  • -

#11 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 10:01 AM

Paste the line of code though, I doubt your line numbers here are going to match the line numbers you have in your IDE.
Was This Post Helpful? 0
  • +
  • -

#12 Hello World 123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 14-July 11

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 10:03 AM

oh ok it occurs when I call the get. method its on line 16
Was This Post Helpful? 0
  • +
  • -

#13 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 10:09 AM

pricePlans is a private property of a class. You also have a semicolon at the end of your for loop...
Was This Post Helpful? 0
  • +
  • -

#14 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 10:17 AM

Basically we need the full version of the code you're running, copy and pasted. Don't retype it except to edit whitespace, because any typos could make it impossible to debug.
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2651
  • View blog
  • Posts: 11,182
  • Joined: 20-September 08

Re: Index Out Of Bounds Exception Error !

Posted 20 July 2011 - 10:27 AM

Quote

            for(i = 0; i < pricePlans.size() i ++) 


is asking for trouble. Use

            for(int i = 0; i < pricePlans.size(); i ++) 

This post has been edited by g00se: 20 July 2011 - 10:28 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2