我看了其他看起来与我有关的问题,但它们并没有“解决”我的问题。我收到了 ArrayIndexOutOfBoundsException,我正在处理的代码正在删除 JList 中名为 contactList 的“联系人”。
我已经实现了一个按钮,该按钮将简单地删除联系人列表中的“联系人”。程序应该做的是,如果按钮删除“广播”,这是联系人列表中的第一个元素,将通过输出显示消息返回错误。否则,它应该简单地从联系人列表中删除联系人。
我现在遇到的问题是,它在删除联系人时会产生arrayIndexOutOfBoundsExc,不仅如此,它还会打印出其他if语句中的错误行。删除广播会输出错误消息,但仍会显示arrary IndexOutofBoundsExc的异常。
另外,我不确定我是否正确执行了第一个 if 语句。我在开头包含了必要的代码以及测试代码后遇到的错误。
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)
基础知识:
如果 JList 中有 5 个元素,则最后一个元素的索引将为 4,列表的大小将为 5。
问题:
for(int i = 0; i <= list.getSize(); i++){
在这里,您说从 0 循环槽到列表的大小(包括列表的大小),因此当我达到
列表的长度时,您将尝试获取具有相同索引的元素,而该元素不存在。
你需要改变