简体   繁体   English

ArrayIndexOutOfBoundsException与JList中的ActionListener

[英]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. 我收到ArrayIndexOutOfBoundsException,并且我正在处理的代码正在删除名为contactList的JList中的“ contact”。

I have implemented a button that will simply delete a "contact" in the contactList. 我实现了一个按钮,该按钮将简单地删除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. 程序应该做的是,如果按钮删除“广播”(contactList中的第一个元素),它将通过输出显示消息返回错误。 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. 我现在遇到的问题是,删除联系人时会产生arrayIndexOutOfBoundsException。 Not only that, it also prints out the error line from the other if statement. 不仅如此,它还会从另一个if语句中打印出错误行。 Removing broadcast outputs the error message but still shows the arraryIndexOutOfBoundsException. 删除广播会输出错误消息,但仍显示arraryIndexOutOfBoundsException。

Also, I'm not exactly sure if I did the first if statement correctly. 另外,我不确定我是否正确执行了第一条if语句。 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. 如果JList中有5个元素,则最后一个元素的索引为4,列表的大小为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. 在这里,您说要从0到包含端值的范围(包括端值)循环,因此当i到达列表的长度时,您将尝试获取具有相同索引的元素,但不存在。

You need to change <= to < 您需要将<=更改为<

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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