简体   繁体   中英

ArrayIndexOutOfBoundsException with ActionListener in JList

I looked at the other questions that look related to mine but they didn't "solve" my problem. I received the ArrayIndexOutOfBoundsException and the code I'm working on is deleting the "contact" in the JList named contactList.

I have implemented a button that will simply delete a "contact" in the contactList. What the program is supposed to do is if the button deletes "Broadcast", which is the first element in contactList, will return an error by outputting a display message. Otherwise, it is supposed to simply remove the contact from the contact list.

The problem I have right now is that it produces the arrayIndexOutOfBoundsException when removing a contact. Not only that, it also prints out the error line from the other if statement. Removing broadcast outputs the error message but still shows the arraryIndexOutOfBoundsException.

Also, I'm not exactly sure if I did the first if statement correctly. I've included the necessary code at the beginning along with the error I got after the code was tested.

private JList<String> listContacts;
listContacts = new JList<String>(controller.getContacts());
// gets contact list from controller class which gets contact list from client class.
private JButton btDeleteUser;

JButton deleteUser = new JButton("Delete User");

deleteUser.addActionListener(new MyButtonListener5());

class MyButtonListener5 implements ActionListener{
    public void actionPerformed(ActionEvent e){
        DefaultListModel<String> list = (DefaultListModel)(listContacts.getModel());
        String contact = listContacts.getSelectedValue();
        int j = -1;
        for(int i = list.getSize()-1; i >= 0; i--){
            if(list.getElementAt(i).equals("Broadcast")){
                controller.displayMsg("[ERROR] You cannot delete broadcast\n");
            }
            else if(list.getElementAt(i).equals(contact) && i != j){
                j = i;
            }
        }
        if(j != -1){
            (DefaultListModel)list.remove(j);
        }
    }
}

[java] Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JList$4 cannot be cast to javax.swing.DefaultListModel
 [java]     at edu.ucsb.cs56.projects.networking.chat.chatclient.view.ClientWindow$MyButtonListener5.actionPerformed(ClientWindow.java:391)
 [java]     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
 [java]     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
 [java]     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
 [java]     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
 [java]     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
 [java]     at java.awt.Component.processMouseEvent(Component.java:6535)
 [java]     at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
 [java]     at java.awt.Component.processEvent(Component.java:6300)
 [java]     at java.awt.Container.processEvent(Container.java:2236)
 [java]     at java.awt.Component.dispatchEventImpl(Component.java:4891)
 [java]     at java.awt.Container.dispatchEventImpl(Container.java:2294)
 [java]     at java.awt.Component.dispatchEvent(Component.java:4713)
 [java]     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
 [java]     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
 [java]     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
 [java]     at java.awt.Container.dispatchEventImpl(Container.java:2280)
 [java]     at java.awt.Window.dispatchEventImpl(Window.java:2750)
 [java]     at java.awt.Component.dispatchEvent(Component.java:4713)
 [java]     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
 [java]     at java.awt.EventQueue.access$500(EventQueue.java:97)
 [java]     at java.awt.EventQueue$3.run(EventQueue.java:709)
 [java]     at java.awt.EventQueue$3.run(EventQueue.java:703)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
 [java]     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
 [java]     at java.awt.EventQueue$4.run(EventQueue.java:731)
 [java]     at java.awt.EventQueue$4.run(EventQueue.java:729)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
 [java]     at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
 [java]     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
 [java]     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
 [java]     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
 [java]     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
 [java]     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
 [java]     at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Basics:

If you have 5 elements in a JList, then the last element's index will be 4, and the size of the list will be 5.

Problem:

 for(int i = 0; i <= list.getSize(); i++){

Here you say to loop trough from 0 to the list's size inclusively, thus when i reaches the length of the list, then you will try to get the element with the same index, wich doesn't exist.

You need to change <= to <

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