简体   繁体   中英

Why am I getting this error array index out of bounds

This is the code for my class so far and without the (int) parse it wont compile without an error for sloppy conversion double to int

with this current code it will compile but not run without an exception error in the monthlyFee method

public class Bank {


    // instance variables=========================================
    // setup new array of BankAccount objects and internal null constructor
    BankAccount [] custAcct = new BankAccount[10];

    // data above here
    //===========================================================
    // methods down below

    // display methods=======================================

    public void printAccounts(){
        for(int i=0; i<custAcct.length; i++) {
            if (custAcct[i]!=null){
                System.out.println(custAcct[i]);
            }
        }   //end loop

    } // end method

    // utility methods=============================

    public void addAccount(BankAccount acct) {
        for(int i=0; i<custAcct.length; i++) {
            if (custAcct[i]==null){
                custAcct[i] = acct;
                break;
            }
        }   // end loop

    } // end method

    public BankAccount getAccount(int acc) {
        return custAcct[acc];
    } // end method

    public void monthlyFee (double acctVal){
        for(int i=0; i<custAcct.length-1; i++) {
            if (custAcct[i]!=null){
                custAcct[i] = custAcct[(int)acctVal];
                break;
            }
        }   // end loop

    } // end method

Your monthlyFee method will throw an IndexOutOfBoundsException on this line:

custAcct[i] = custAcct[(int)acctVal];

if acctVal is higher than the length of custAcct (in this case 10) or lower than 0. From the comment you posted to your question, I see you're calling monthlyFee with -2.95, this is the source of your IndexOutOfBoundsException .

Your addAccount method may contain a design flaw, too: the way it is currently coded, it fills all of the empty spots in the custAcct array with the BankAccount that it was passed as an argument. If you only want to add the account to the first empty position, use this:

public void addAccount(BackAccount acct) {
    for(int i = 0; i < custAcct.length; i++) {
        if(custAcct[i] == null) {
            custAcct[i] = acct;
            return;
        }
    }
}

I am thinking this should be coming from here (perhaps sending snippet of stack trace will help)

            custAcct[i] = custAcct[(int)acctVal];

What value is being passed for acctVal, if the value is less than 0 or greater than equal to custAcct.length then there is a potential of Array out of bound exception. The other issue can be custAcct[i] is not initialized and there is a potential of null pointer exception. Can tell better if you give the stack trace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM