0

When I try to reset a list Marked I get a Null Pointer Exception.

The problem must be cause I never said what B and C is. (Boolean B, Integer C) And I don't know how to do this.

Here is a part of my code :

Marked[] marked;


//Create list marked!
public class Marked<B,C>{
    public B bool;
    public C comp;
}

public Graph(int N)
{

    //Fill marked with false and 0
    marked = new Marked[N];
    for(int i=0;i<N;i++){

        marked[i].bool = false;
        marked[i].comp=0;
    }

2 Answers 2

5

Creating an array of Marked doesn't actually initialize the elements in the array:

marked = new Marked[N];
for(int i = 0; i < N; i++) {
    marked[i] = new Marked<Boolean, Integer>();
    marked[i].bool = false;
    marked[i].comp = 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@STUDENT_LIFE Don't forget to accept the answer if it has helped you =)
2

The statement marked = new Marked[N]; creates a new array of Marked objects with N elements, but does not initialize them. Each element in this array would be null. You need to manually initialize them by calling the constructor.

So, your for loop should look like this:

for(int i=0;i<N;i++) {
    marked[i] = new Marked();
    marked[i].bool = false;
    marked[i].comp=0;
}

1 Comment

Thank you both for your fast reply! :)

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.