【问题标题】:"AWT-EventQueue-0" java.lang.IndexOutOfBoundsException, (need to add null to the arrayList), 2-dimensional ArrayList"AWT-EventQueue-0" java.lang.IndexOutOfBoundsException,(需要给arrayList加null),二维ArrayList
【发布时间】:2016-03-08 17:31:40
【问题描述】:

这个问题已经面临很多时间了,我知道,我遇到了这些问题,但无法理解我的问题。这是我下面给出的代码的一部分,

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(因为有二维ArrayList)。 changeListener 随时更改索引。

其中nf_list 是一个包含 4 个元素的字符串的 ArrayList。 但它抛出异常。 我在构造函数中初始化了network_slicesnetwork_slices = new ArrayList&lt;Slice&gt;();

【问题讨论】:

  • 在哪里声明了nf_list,你在哪里初始化了network_slices?发布完整的代码和异常跟踪。
  • 我在问题中进行了编辑。
  • 您正在将索引存储在状态中。当您将 network_slices 设置为新的 ArrayList 时,它的新大小为零。因此,如果其中没有任何元素,任何对 get(index) 的调用都会失败。您需要添加一个检查以确保您的 network_slices ArrayList 不为空。
  • 你在谈论 nfs_slices,因为当 network_slices 调用 get(index).它可以正常工作,我认为 nfs_slices 不能调用 get (index)。

标签: java arrays arraylist multidimensional-array


【解决方案1】:

你不能保证 network_slices 确实在被访问的索引中包含一个对象,因此你有一个空指针异常。

至少在对其进行任何操作之前检查它是否不为空......

【讨论】:

  • network_slices 不是空的,它现在有 4 个 JPanel,我在下面给出这个,[network.slicing.Slice[,0,0,0x0,invalid,hidden,layout=java.awt .FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=550,height=600]]] [network.slicing.Slice[, 2,25,550x634,....], [network.slicing.Slice[,0,0,0x0,...]]] 等等,
  • 但是你确实有空指针异常...调试并实时检查索引值和数组内容...
  • nfs_slices 或 network_slices 的索引值? network_slices 是一个空数组列表,nfs_slices 是二维数组列表。
  • 您不能在空的 ArrayList 上调用 get(index)。它会失败的。确保您的 ArrayList 首先在该索引处有一个元素。你需要检查它的大小。
  • 非常感谢@ManoDestra,我想我想出了解决方案。我将发布作为答案,
【解决方案2】:

我在二维数组的每一行中都添加了空列表。我在构造函数中添加了它,这样在使用它之前它就不会为空,并且我可以调用get(index),

for (int i=0; i<10; i++){
        nfs_slices.add(new ArrayList<String>());
        for(int j=0; j<nf_number; j++)
            nfs_slices.get(i).add(null);
    }

现在它可以调用 get(index) 并将元素放在特定位置。 谢谢大家帮助我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 2021-11-27
    • 2017-05-09
    • 2015-01-12
    相关资源
    最近更新 更多