1

This seems a bit strange to me. I created a static Integer array and then I am trying to assign a value to it. But I get a Null pointer exception at the degree[i] = 0 line.

Since I am not reading the value before assigning it, I do not understand why the NullPointer exception.

private static Integer[] degree;
public static void initDegree(int num_of_vertices) throws Exception{
    for (int i = 0; i < num_of_vertices; i++) {
        degree[i] = 0;
    }
}
1
  • 5
    you have not initialized the array. Commented Apr 20, 2014 at 22:04

1 Answer 1

3

You need to initialize the array.

ex

degree = new Integer[5];

Otherwise, the array itself is just a null.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! But how do I initialize it if I do not know the size beforehand?
@Yathi use ArrayList
@Yathi or initialize it before for loop, like: degree = new Integer[num_of_vertices];

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.