当我尝试调用IndexOutOfBoundsException
方法时,我收到tableUpdate
我认为使用set()
方法的方式有问题。我试过像这样a -1
放int row = members.size() -1
,但这并没有解决它。你能帮我么?
class MyTableModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Member> members = new ArrayList<Member>();
public MyTableModel(List<Member> members) {
this.members = members;
}
public void addMember(Member m) {
members.add(m);
int row = members.size() - 1;
fireTableRowsInserted(row, row);
}
public void removeMemberFromTable(int row) {
members.remove(row);
fireTableRowsDeleted(row, row);
}
public void tableUpdate(int row, Member m) {
row = members.size();
members.set(row, m);
fireTableRowsUpdated(row, row);
}
public List<Member> getMembers() {
return new ArrayList<Member>(members);
}
@Override
public int getColumnCount() {
return 8;
}
@Override
public int getRowCount() {
return members.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Member m = members.get(rowIndex);
switch (columnIndex) {
case 0:
return m.getMemberId();
case 1:
return m.getfName();
case 2:
return m.getlName();
case 3:
return m.getAddress();
case 4:
return m.getZipCode();
case 5:
return m.getCity();
case 6:
return m.getEmail();
case 7:
return m.getPhoneNo();
default:
return "";
}
}
public String getColumnName(int column) {
switch (column) {
case 0:
return "MedlemsID";
case 1:
return "Fornavn";
case 2:
return "Efternavn";
case 3:
return "Adresse";
case 4:
return "Postnummer";
case 5:
return "By";
case 6:
return "Email";
case 7:
return "Telefonnr";
default:
return "";
}
}
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
case 4:
return String.class;
case 5:
return String.class;
case 6:
return String.class;
case 7:
return String.class;
default:
return String.class;
}
}
EXCEPTION
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.set(ArrayList.java:397)
at gui.MyTableModel.tableUpdate(ShowMembersGUI.java:179)
at gui.EditMemberGUI$1.actionPerformed(EditMemberGUI.java:108)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:2)
您想要在最后一个之后替换元素。这没有意义。也许你想检查索引是否在边界内,如下所示:
public void tableUpdate(int row, Member m) {
if (row < members.size()) {
members.set(row, m);
fireTableRowsUpdated(row, row);
}
}
答案 1 :(得分:0)
列表为零索引。这意味着最后一个元素的大小为1,而不是大小。
row = members.size();
members.set(row, m); // tries to "overwrite" instance at index = size,
// which is always greater than the last index.
这将始终抛出IndexOutOfBoundsException,因为它没有索引大小,因为它是零索引。