【问题标题】:ArrayList<String> gives NullPointerException When addingArrayList<String> 添加时给出 NullPointerException
【发布时间】:2014-04-14 19:45:52
【问题描述】:

我知道大多数人都犯了未初始化 Arraylist 的错误,这就是他们得到 NullPointerException 的原因。

但是在我的课堂上我已经初始化了我的 Arraylist,当我尝试添加到它时我仍然得到异常。

protected int V;
protected int E;
protected boolean[][] adjacencyMatrix;
public int i;
public int[] num;
public ArrayList<String> edges;

public SimpleGraph(int V) {
    this.V = V;
    this.E = 0;
    this.adjacencyMatrix = new boolean[V][V];
    edges = new ArrayList<String>();
    num = new int[V];
}

public void DFS(int V) {

    num[V] = i++;
    for(int u = 0; u < getV(); u++)
    {
        if(adjacencyMatrix[V][u] == true && u != V)
        {
            if(num[u] == 0)
            {
                Integer temp = new Integer(num[u]);
                edges.add("anything");
                DFS(u);
            }
        }
    }

}

您可以假设我已正确添加到 adjacencyMatrix。

为什么我在尝试添加到 Arraylist 时会收到 NullPointerException?

【问题讨论】:

标签: java arraylist nullpointerexception add


【解决方案1】:

你声明了queue ArrayList

protected ArrayList<Integer> queue;

但是初始化了一个edges ArrayList

 edges = new ArrayList<String>();

所以尝试初始化queue ArrayList

queue = new ArrayList<Integer>();

【讨论】:

  • 是的,但是 OP 添加到 edges。因此,犯罪并没有解释edges 是什么,而不是构建了错误的列表。
  • 我试图弄清楚“队列”的使用位置和“边缘”的首次声明位置。好收获
【解决方案2】:

看起来在你的类变量中,你有

protected ArrayList&lt;Integer&gt; queue,

但是在SimpleGraph() 构造函数中,你有

edges = new ArrayList&lt;String&gt;();

edges 应该是 queue,反之亦然。

此外,edges 应该是 ArrayList&lt;String&gt;,如果您的意思是它与 queue 的类型相同。

【讨论】:

  • 感谢您的回复,这只是一个简单的错误,我将队列用于其他功能,我确实有一个边的 Arraylist
【解决方案3】:
package test_001;

import java.util.ArrayList;

public class Vanste {

    protected int V = 5;
    protected static int E;
    protected static boolean[][] adjacencyMatrix;
    public static int i = 0;
    public static int[] num;
    public static ArrayList<String> edges;

    public static void SimpleGraph(int V) {
        E = 0;
        adjacencyMatrix = new boolean[V][V];
        edges = new ArrayList<String>();
        num = new int[V];
    }

    public static void DFS(int V) {

        num[V] = i++;
        for(int u = 0; u < V-1; u++)
        {   
            /*System.out.println("A1");
            if(adjacencyMatrix[V-1][u] == true && u != V-1)
            {   System.out.println("A1");
                if(num[u] == 0)
                {*/
                    Integer temp = new Integer(num[u]);
                    edges.add(temp.toString() + ":");
                    DFS(u);
                //}
            //}
        }

    }

    public static void main(String[] args) {
        SimpleGraph(15);
        DFS(5);
        for(int i =0; i< edges.size(); i++) {
            System.out.println("" + edges.get(i) + "\n");
        }

    }

}

这个例子工作正常。在将项目添加到列表之前,您的逻辑有问题。 顺便说一句,您正在使用和递增从未初始化的变量 i。 这种说法从来都不是真的。 (adjacencyMatrix[V][u] == true) 您能否提供一个工作函数调用而不是我的 SimpleGraph(15) 和 DFS(5)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-08
    • 2023-03-22
    • 2014-04-02
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多