简体   繁体   中英

why can i not access a class array index

I keep getting a NullPointerException when I try to access elements of an array I created and don't know why. The code that throws:

    TreeNode[] list1;
    list1 = new TreeNode[1000];
    list1[0].edges = new EdgeNode(1); //Throw line

but if I do this, it works:

    TreeNode[] list1;
    list1 = new TreeNode[1000];
    list1[0] = new TreeNode();
    list1[0].edges = new EdgeNode(1);

and I don't know why. Obviously I could for loop through the whole array and make new elements but doesn't that defeat the point of new? Also, if relevant, I have defined the default constructor for TreeNode.

You actually answered your own question: elements of an array should be individually initialized. By default they are null for array of objects . Hence NullPointerException .

This is exactly how java works. When you create an array, all elements are initialized to null , and you need to initialize them. That is because there is no way it could find out whether you just wanted them constructed with the default constructor, or with a different constructor with constant parameters, or a different constructor with varying parameters, or null, or what. So it just initializes the array to null .

After you say list1 = new TreeNode[1000] ,

list1 is a new array full of nulls.

So you have to loop through and initialise it

It does not defeat the point of new, because when you say new A[10000], that is allocating a new array, not its' elements. Saying list1[0] = new TreeNode(); allocates an element.

I agree this isn't elegant but that is java for you. :)

When you call new TreeNode[1000] you are instantiating an array of references, not the objects themselves. This is normal since the compiler can't just assume which constructor to call. Imagine if you had a class that didn't have a no-argument constructor: how do you think the compiler would have to instantiate it in that case?

There are two Initialization involved in this scenario

  1. Initialization of the Array
  2. Initialization of Array Elements

    new TreeNode[1000] only initializes Array not Array Elements.

list1 = new TreeNode[1000];

This is only creating a new TreeNode array. The array itself is an object. So don't let the new keyword fool you. So with the above code, all you have is an array of TreeNode type. That only holds values of a TreeNode type. Does not give it any TreetNode` value. You must do that explicity yourself with code.

When you create new array of objects it is by default filled with null s so when you are executing

list1[0].edges

in reality you are trying to execute

null.edges

which is incorrect since null doesn't have edges .

There are few reasons why arrays are not filled with new objects after being created:

  1. In many (if not most) cases we want to place in array objects that already exists, so creating new ones would be waste of time.
  2. Which constructor should compiler use to create objects that would fill array?
  3. What arguments should be used in such constructor?
  4. Remember that array can be array of classes that can't be instantiated like abstract classes or interfaces. How would compiler fill such array? Which subclass should be used?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM