-2

I know most people make the mistake of not initializing the Arraylist and that's why they get the NullPointerException.

But in my class i have initialized my Arraylist and I still get the exception when I try to add to it.

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);
            }
        }
    }

}

You can assume that I have correctly added to the adjacencyMatrix.

Why do I get a NullPointerException when I try adding to the Arraylist?

beckinho
  • 33
  • 11
  • First of all I suggest you put more parentheses inside your "if" statement containing two tests. Even if it doesn't confuse the program (which it might) it is difficult to follow. – DerStrom8 Apr 14 '14 at 19:49
  • 1
    I am confused: You declared an ArrayList called queue, but it is never used? – donfuxx Apr 14 '14 at 19:50
  • 2
    where is the class variable edges defined ? – Kakarot Apr 14 '14 at 19:50
  • 1
    Please add the stack trace to your question. – Duncan Jones Apr 14 '14 at 19:51
  • @donfuxx Thanks for your reply, that was just a simple mistake, I use the queue for other functionalities, I do have a Arraylist of edges – beckinho Apr 14 '14 at 19:55
  • We know that `num[u] == 0`, does changing the contents of the `if(num[u])...` statement to `edges.add("0:");DFS(u);` change the result? – Colin D Apr 14 '14 at 19:56
  • @Kakarot I made a mistake when I copied my code, now you can see where I define it – beckinho Apr 14 '14 at 19:57
  • You should post your exception provided by logcat. And please remove all that fancy logic and make a little example which only adds a string to the ArrayList. Have you ever debugged anything? – vanste25 Apr 14 '14 at 19:58
  • @ColinD the value of num[u] is not always 0, so I need that statement – beckinho Apr 14 '14 at 19:59
  • @vanste25 I tried just adding a simple string, it still gives me the exception – beckinho Apr 14 '14 at 20:00
  • @beckinho the nullpointer is coming from the block when `num[u] == 0`, inside the block, you then use `num[0]` again to create a string. The `temp.toString()+":"` is always going to generate `0:` from what I can tell. Is that wrong? I was just suggesting your inline all that logic. – Colin D Apr 14 '14 at 20:02
  • Make a little compilable example, and post it in pastebin. In that way, someone could copy code and compile it in order to find a mistake. – vanste25 Apr 14 '14 at 20:02
  • @vanste25 Don't encourage use of pastbin. Small examples should be included in questions. – Duncan Jones Apr 14 '14 at 20:03
  • @ColinD I agree on your remark, yes that is a mistake, but that is not my problem, my problem is that even when I try to add a simple string the program gives me the exception – beckinho Apr 14 '14 at 20:04
  • 1
    Post the stack trace already. – Sotirios Delimanolis Apr 14 '14 at 20:05
  • do you have another default constructor in your class ? – Kakarot Apr 14 '14 at 20:06
  • Im so sorry @SotiriosDelimanolis I dont know what is a stack trace :/ – beckinho Apr 14 '14 at 20:07
  • 1
    [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – Sotirios Delimanolis Apr 14 '14 at 20:08
  • 1
    @beckinho Then Google it? One short moment of searching and you'd find out what it was. Please put a bit of effort in yourself. – Duncan Jones Apr 14 '14 at 20:08

3 Answers3

1

You declared a queue ArrayList

protected ArrayList<Integer> queue;

But initialized a edges ArrayList

 edges = new ArrayList<String>();

So try to initilize the queue ArrayList

queue = new ArrayList<Integer>();
Salah
  • 8,567
  • 3
  • 26
  • 43
0

Looks like in your class variables, you have

protected ArrayList<Integer> queue,

but in the SimpleGraph() constructor, you have

edges = new ArrayList<String>();

edges should be queue, or vice versa.

Additionally, edges should be an ArrayList<String> if you meant it to be the same type as queue.

geoff
  • 2,251
  • 1
  • 19
  • 34
  • Thanks for your reply, that was just a simple mistake, I use the queue for other functionalities, I do have a Arraylist of edges – beckinho Apr 14 '14 at 19:55
0
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");
        }

    }

}

This example works ok. Something is wrong with your logic before adding items to the list. Btw, you are using and incrementing variable i which is never initialized. This statement is never true. (adjacencyMatrix[V][u] == true) Could you please provide me a working function calls istead of mine SimpleGraph(15) and DFS(5).

vanste25
  • 1,754
  • 14
  • 39