I am having trouble creating and using objects to type List<Integer>
. When I run the following code, I get a NullPointerException
because the Object isn't initialized.
import java.util.List;
public class RBTree {
public static class Tree {
public List<Integer> parent;
public List<Integer> right;
public List<Integer> left;
public List<Integer> data;
public List<Boolean> black;
}
public static void main (String[] args){
Tree rb =new Tree();
rb.data.add(-1);
rb.left.add(-1);
rb.right.add(-1);
rb.parent.add(-1);
rb.black.add(Boolean.TRUE);
}
}
The compiler also gives me errors unless I add static
to the public static class Tree
line, but I don't want Tree
to be static
i.e. immutable. I need to be able to use a Tree more-or-less like a struct
in C.