所以我打電話來自另一個類的方法makeCard(String info)。它在第一次運行時運行正常,但第二次我調用它似乎運行兩次的方法,這會創建一個StringIndexOutOfBoundsException運行時錯誤,我似乎無法弄清楚爲什麼。我對Java有點新,所以我可能會漏掉一些顯而易見的東西,但我頭腦中的邏輯表示,如果調用一次,它應該只運行一次。希望有人能指出我的錯誤。我的方法運行兩次,它只被調用一次
這裏是方法:
public void makeCard(String info){
cInfo = new StringBuffer(info);
int i = 0;
while(cInfo.charAt(i)== ' '){
cInfo.deleteCharAt(i);
}
while(cInfo.charAt(cInfo.length()-1)== ' '){
cInfo.deleteCharAt(cInfo.length()-1);
i--;
}
seperateValues();
makeObject();
}
和這裏是它被稱爲:
@Override
public void actionPerformed(ActionEvent e) {
MainWindow mw = new MainWindow();
CardBreakdown cb = new CardBreakdown();
if("submit".equals(e.getActionCommand())){
cb.makeCard(cardInfo.getText());
mw.removeAddPanel();
cardInfo.setText("");
}
}
預先感謝您的任何幫助,您可以提供
是這樣的錯誤: 線程「AWT-EventQueue-0」中的異常java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:0
可以發佈確切的錯誤消息嗎?在螺紋 – FaddishWorm
異常 「AWT-EventQueue的-0」 java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:0 \t在java.lang.StringBuffer.charAt(未知來源) \t在CardBreakdown.makeCard(CardBreakdown.java:24 ) \t在EnterCard.actionPerformed(EnterCard.java:62) \t在javax.swing.AbstractButton.fireActionPerformed(來源不明) \t在javax.swing.AbstractButton中的$ Handler.actionPerformed(來源不明) \t在javax.swing中.DefaultButtonModel.fireActionPerformed(未知源) – mig
我不會使用StringBuffer,永遠。我會使用'info.trim()'這將做你想做的。 –