Results 1 to 8 of 8
  1. #1
    sh4rif is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Index out of bound exception

    Hello everyone

    I have a question here, which i can't resolve by myself as i am a beginner to java. my situation is explained below.
    I have got two Java file (Bank.java and Branch.java),
    when I create a new bank I add a branch to that it i.e. bank_name.addBranch(Branch aBranch)
    there is also a counter variable which counts how many branches has been added to the bank.
    There is a class level variable of Branch (array_of_branches i.e Branch[] bList) as wekk, which i initiliaze in addBranch function.
    eventually i would initiliaze this branch array in constructor but for now this is what i want..


    public class Bank{
    Branch[] bList;
    public void addBranch(Branch aBranch){
    int numBran = this.getNumBranches() + 1;
    setNumBranches(numBran);
    bList = new Branch[numBran];
    bList[numBran] = aBranch; // i gto error on that line array out of bound exception
    }
    }

    what i am trying to do here is, I want to add a branch into that bank every time addBranch function is being called.

    please please help me i put the relevnt code below if anyone can help me please please.... thanks


    please help

    Sharif

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,567
    Rep Power
    23

    Default Re: Index out of bound exception

    What is the value of numBran when the red statement is executed?
    The JVM says that its value is not within the bounds of the size of the array.
    Add a println() statement just before the red statement to show the variables value.
    Then check the code's logic to see why it got that invalid value.

    Remember arrays are zero based. The first element is at 0, the last element is at the size of the array minus one.

  3. #3
    sh4rif is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Index out of bound exception

    yeh i have done that too (prinltln() statement) and it show the correct value i called it three time and the value is in the end is three. the vaule of numBran is start from 1 and every time you call that function it add 1 one to its previous value. kind of numBran++

  4. #4
    sh4rif is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Index out of bound exception

    in other words my question is to explain how to declain a dynamic array of object.
    if you do the following
    Branches[] bList = new Branch[5];
    in above example i've declarid my Branch array has a length of five

    but in some cases you dont know how many branches you gonna have and you put a dynamic value
    i.e
    count_branches = getBranches.length+1; //get the the available length of branchs i.e 3 or 4
    Branches[] bList = new Branchcount_branches ];

    in the case above what i am trying to explain is, dont fix the value of array to any integer value start from 0 or 1 and if you add a branch increse the length of that array to 2 if you add an another branch, make it 3...you know what i am trying to explain here....

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,567
    Rep Power
    23

    Default Re: Index out of bound exception

    Did you understand this?
    Remember arrays are zero based. The first element is at 0, the last element is at the size of the array minus one.

    What is the size of the array and what is the value of the index? They can not be equal.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,567
    Rep Power
    23

    Default Re: Index out of bound exception

    how to declain a dynamic array of object.
    There is no such thing as a dynamic array. Their size is set when they are defined.

  7. #7
    sh4rif is offline Member
    Join Date
    Dec 2011
    Posts
    16
    Rep Power
    0

    Default Re: Index out of bound exception

    Thank you Norm but another one relating to this question,
    if I have a set the size of an array to any number lets say 10 for instance i.e Branch bList = new Branch[10],
    (I know array index starts from zero onwards and 9 would be the last item in the array)

    I have added a few branches to this array lets assume 3 or 4 or 5 any number but less then its actual size,
    my question is how would i know how many elements of array have been used and how many left to use

    if i use
    bList.lenth that gives me an ans of 10
    but i want the answer to be less then becauose array is not fully used yet.

    help me please
    thanks

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,567
    Rep Power
    23

    Default Re: Index out of bound exception

    how would i know how many elements of array have been used
    You would need a variable to hold the number of elements that had been used.

Similar Threads

  1. Replies: 5
    Last Post: 11-22-2011, 12:42 PM
  2. array out of bound exception
    By farahm in forum New To Java
    Replies: 6
    Last Post: 12-19-2010, 09:10 PM
  3. Array index out of bound exception error
    By rahulkrishnanr in forum Threads and Synchronization
    Replies: 7
    Last Post: 10-12-2010, 05:57 PM
  4. Array index Out of bound Exception
    By nitin_daviet88 in forum New To Java
    Replies: 9
    Last Post: 07-28-2010, 05:32 AM
  5. Array Index Out of bound exception
    By abhijit in forum Networking
    Replies: 7
    Last Post: 09-25-2009, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •