[英]If Statement Executing When False
在運行(在運行或調試模式下)我的項目時,我得到一個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]
看起來它運行正常,然后再次運行並崩潰。 我錯過了什么(可能是顯而易見的事情)? 感謝您的時間和任何幫助。
很清楚:
兩者:假
兩者:假
_selectedIndex:-1
lstContacts.getSelectedIndex:-1
線程“AWT-EventQueue-0”中的異常java.lang.ArrayIndexOutOfBoundsException:-1
所選索引為-1與執行相同:
_contactList.get(-1);
請記住,幾乎java中的所有集合都需要索引> = 0。
我認為你應該修改你的條件,以檢查用戶是否有(或沒有)選擇列表中的項目,以便你可以處理錯誤。 就像是:
if(lstContacts.getSelectedIndex() >= 0){
_selectedIndex = lstContacts.getSelectedIndex();
PersonalContact sc = (PersonalContact)_contactList.get(_selectedIndex);
showContact(sc);
}
希望能幫助到你。
快樂的編碼!
EFRA
正如您對帖子的評論所表明的那樣,在您使用JList.getSelectedIndex()檢查其值時,某些其他線程可能正在更新您的列表並取消選擇所有內容。 您沒有顯示所有代碼,但您可能正在使用lstContacts執行某些操作,即清除列表中的所有選擇。
由於您要響應特定事件,因此請不要檢查列表中選定的值,這些值在多線程環境中可能會從一個瞬間更改為下一個瞬間。 而是使用ListSelectionEvent.getFirstIndex())
檢查事件選擇的值,該事件應該是常量。
您可能會發現Oracle的Swurrency Concurrency教程很有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.