简体   繁体   中英

How do I fix this index out of bound error

Main Class

        FileDealer.modify(t2, 0, "99999");
        FileDealer.modify(t2, 0, "88888");

FileDealer Class (Method: Modify)

public static void modify(Ticket t, int type, String newInformation) throws MacAddressLengthException, IOException {
        int index = ticketList.indexOf(t);
        Ticket toModify = null;
        toModify = ticketList.get(index);
        switch (type) {
        case 0: // adding mainID
            toModify.setMainID(Utility.convertInt(newInformation));
            break;
        case 1: // adding assetID
            toModify.setAssetID(Utility.convertInt(newInformation));
            break;
        case 2: // adding macAdd
            toModify.setMacAdd(newInformation);
            break;
        case 3: // edit
            toModify.setNote(newInformation);
            break;
        case 4: // delete
            ticketList.remove(index);
        }
        updateFile();
        readFile();
    }

when the modify method is called the first time it is able to edit the ticket details, but when it is called the 2nd time, the int index = ticketList.indexOf(t) from the method returns a "-1" even though nothing changed, and it gives me an exception for the index being out of bound. How do I change that -1 to show the right index?

Probably the equals method of the Ticket class depends in any way of the mainID field, and it seems that you modify that field in the modify method.

As a consequence, the second time you invoke modify , indexOf returns -1 because equals evaluates to false .

This issue occurs if t2 is not in the ticketList - so there are two equal objects: one is t2 and the other is in the ticketList :

static List<Ticket> ticketList = Arrays.asList(
    new Ticket(1, 1, "one", "one"), 
    new Ticket(2, 2, "two", "two"), 
    new Ticket(3, 3, "drei", "drei")
);
// ...
Ticket t2 = new Ticket(2, 2, "two", "two");

modify(t2, 0, "99999");
modify(t2, 0, "88888");

Most likely, methods equals and hashCode are overridden in class Ticket and they use the values of the member fields to define equality.

Then, method modify updates existing value toModify within the list, and possibly the ticketList is completely rebuilt inside calls to updateFile , readFile .

Thus, t2 becomes outdated because ticketList does not contain a ticket with old mainID any longer and indexOf returns -1.

This can be overcome if the reference t2 is initially bound to some object within the ticketList -- then it becomes updated outside method modify :

Ticket t2 = ticketList.get(1);
modify(t2, 0, "99999");
modify(t2, 0, "88888");

You need to check if the index is equal to -1 then print a clear message and skip the process cause the ticket will not be there. indexOf returns -1 if there is no matching, so you could add this if:

if(index < 0){
 System.out.print("ticket not exists!");
 break;
}

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