运行(在运行或调试模式下)我的项目时,我收到一个 ArrayIndexOutOfBounds 错误,这是有道理的。什么不是我检查索引是否> = 0,尽管它说索引是-1,但if中的代码仍然运行。
代码:
...
// Contact List
lstContacts = new JList();
lstContacts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lstContacts.setPreferredSize(new Dimension(200, 200));
lstContacts.setMinimumSize(new Dimension(50, 50));
_contactList = _dbi.GetContactList();
_selectedIndex = -1; // An int declared earlier
lstContacts.setListData(_contactList.toArray());
lstContacts.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
System.out.println();
System.out.println("lstContacts.getSelectedIndex: " + lstContacts.getSelectedIndex());
System.out.println("!e.getValueIsAdjusting: " + (!e.getValueIsAdjusting()));
System.out.println("getselectedindex > 0: " + (lstContacts.getSelectedIndex() > 0));
System.out.println("Both: " + (!e.getValueIsAdjusting() && (lstContacts.getSelectedIndex() > 0)));
// Filter out mid-actions
if(!e.getValueIsAdjusting() && (lstContacts.getSelectedIndex() > 0))
{
if(pnlDetail.isVisible())
{
saveCurrentContact();
}
else
{
pnlDetail.setVisible(true);
}
System.out.println(" Both: " + (!e.getValueIsAdjusting() && (lstContacts.getSelectedIndex() > 0)));
_selectedIndex = lstContacts.getSelectedIndex();
System.out.println(" _selectedIndex: " + _selectedIndex);
System.out.println(" lstContacts.getSelectedIndex: " + lstContacts.getSelectedIndex());
PersonalContact sc = (PersonalContact)_contactList.get(_selectedIndex); //crashes here
showContact(sc);
}
}
});
...
我首先在列表中插入了三个虚拟联系人。单击一个运行正常,但单击另一个会引发错误。在下面的错误中,我单击了第二个条目。
控制台输出:
...
lstContacts.getSelectedIndex: 2
!e.getValueIsAdjusting: true
getselectedindex > 0: true
Both: true
Entry ID [2] modified.
lstContacts.getSelectedIndex: -1
!e.getValueIsAdjusting: true
getselectedindex > 0: false
Both: false
Both: false
_selectedIndex: -1
lstContacts.getSelectedIndex: -1
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at main.ContactPanel$2.valueChanged(ContactPanel.java:203)
at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
... [continued]
看起来它运行正确,然后再次运行并崩溃。我错过了什么(可能是显而易见的事情)?感谢您抽出宝贵的时间以及您能提供的任何帮助。