-2

I have found many related post here but couldn't get my answer.Why this run-time error ?

static List<Integer>[] adj = (List<Integer>[]) new ArrayList[1000];

    public static void main(String[] args) {
        int edge, u, v, source;
        Scanner input = new Scanner(System.in);
        edge = input.nextInt();
        for (int i = 0; i < edge; i++) {
            u = input.nextInt();
            v = input.nextInt();
            adj[v].add(u); // Null pointer Exception
            adj[u].add(v); // Null pointer Exception
        }
1

4 Answers 4

3

First you need to initialize each elements of your array. Because until you do this, your reference in the array are not pointing to any object.

So, before that for-loop, you can add this one, to initialize your List inside the Array: -

for (List<Integer> elem: adj) {
    elem = new ArrayList<Integer>();
}

Also, it would be better if you have List of List rather than an array of List. So, you can declare your list as: -

static List<List<Integer>> adj = new ArrayList<List<Integer>>();

An advantage of using an ArrayList is that, you don't have to limit your size at the beginning. So, you can add any number of elements. But, if it is a requirement to create a fixed size list, you can pass the size parameter in your ArrayList constructor.

And then you need to change your element adding code from: -

adj[v].add(u);

to: -

adj.get(v).add(u);
6
  • +1 for suggesting using a list of list. I think that would be the way to go. Commented Oct 24, 2012 at 19:04
  • @Sujay. Yeah using list of list is always a better option than a 2-D array or an array of list. :) Commented Oct 24, 2012 at 19:05
  • okay, using static List<List<Integer>> adj = new ArrayList<List<Integer>>(100); is a nice idea.I tried to use it before but can't figure out the adj[u].add(v); portion. What will be this code then? Commented Oct 24, 2012 at 19:08
  • 1
    Just some pedantry: you mean 'initialize', not 'instantiate' in your first sentence. To 'initialize' something is to assign a value to it, when it has not yet had one assigned. To 'instantiate' something (generally a class) means to create an instance of it. Incidentally, we're instantiating ArrayList objects here to populate the array with, but we're still initializing the array elements, not instantiating them. May I suggest you edit your answer? Commented Oct 24, 2012 at 19:10
  • IndexOutOfBoundsException on the line adj.get(u).add(v); :( Commented Oct 24, 2012 at 19:17
2

That's because you don't allocate adj[v]. You can't call the add method on null.

You could do

   for (int i = 0; i < edge; i++) {
        u = input.nextInt();
        v = input.nextInt();
        if (adj[v]==null) adj[v] = new ArrayList();
        adj[v].add(u); 
        if (adj[u]==null) adj[u] = new ArrayList();
        adj[u].add(v); 
    }
1

You've created an array for sure but you've not allocated each element of the array. As such when you're trying to add to the non-existant elements, you experience a NullPointerException.

As a side-note, what you're trying to achieve would look a lot better if you create a List of List. In other words:

List<List<Integer>> yourList = new ArrayList<List<Integer>>();

You can then initialize each of your individual list inside yourList and then put elements into it.

0
List<Integer>[] adj = (List<Integer>[]) new ArrayList[1000];

says adj is a reference to 1000 ArrayList references. You need to initialize each of those ArrayLists as well.

private final int MAX = 1000;
static List<Integer>[] adj = (List<Integer>[]) new ArrayList[MAX];
public static void main(String[] args) {
    for (int i = 0; i < MAX; i++){
       adj[i] = new ArrayList();
    }

    int edge, u, v, source;
    Scanner input = new Scanner(System.in);
    edge = input.nextInt();


    for (int i = 0; i < edge; i++) {
        u = input.nextInt();
        v = input.nextInt();
        adj[v].add(u); // Null pointer Exception
        adj[u].add(v); // Null pointer Exception
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.