这个问题已经面临了很多时间,我知道,我遇到了这些问题,但couldnt了解我的问题。这是下面给出我的代码的一部分,“AWT-EventQueue-0”java.lang.IndexOutOfBoundsException,(需要向arrayList添加null),二维ArrayList
public ArrayList<Slice> network_slices;
public ArrayList<ArrayList<String>> nfs_slices= new ArrayList<ArrayList<String>>();
public int index;
private class TabSelect implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JTabbedPane source = (JTabbedPane) e.getSource();
if (source.getSelectedComponent() != null) {
index = source.getSelectedIndex();
}
}
}
private int count = 0;
private class AddNetworkFuncitons implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String buttonselected = (String) e.getActionCommand();
JButton button = new JButton(buttonselected);
network_slices.get(index).add(button).setVisible(true);
int c =nf_list.indexOf(buttonselected);
System.out.println(index);
System.out.println(c);
nfs_slices.get(index).set(c,nf_list.get(c));
System.out.println(nfs_slices);
// System.out.println(nfs_slices.get(index));
count ++;
}
}
每当被执行的操作,它应该增加buttonselected
字符串对应index
列表中的ArrayList(因为有2维的ArrayList)。索引由changeListener随时更改。
其中nf_list
是一个包含4个元素的String的ArrayList。 但它引发异常。 我在构造函数中初始化了network_slices
,network_slices = new ArrayList<Slice>();
在哪里声明了'nf_list',并且在哪里初始化了'network_slices'?发布完整的代码和异常跟踪。 – saurav
我在问题中编辑。 – nothingSpecial
您正在将索引存储在状态中。当您将network_slices设置为新的ArrayList时,其新大小为零。所以,如果没有任何元素,任何调用get(index)都会失败。你需要添加一个检查来确保你的network_slices ArrayList不是空的。 – ManoDestra