Array Object and Ordinary object Initialization

后端 未结 1 2013
广开言路
广开言路 2021-01-29 16:15

I have 3 objects root1,root2,root3 where they are present in root[] (Array Object) which is a collection of objects.

I have initialized the

相关标签:
1条回答
  • 2021-01-29 17:13

    I have 3 objects root1,root2,root3

    No you don't. You have three variables, and they are all null.

    where they are present in root[]

    Nothing is present in root[] except three nulls when you initialize it.

    I have initialized the Node class using root[0]

    You have instantiated the Node class, and stored the reference into root[0].

    // It is printing null value.
    

    With this code it should be throwing NullPointerException.

    root[0]=root1
    

    No it doesn't. You overwrote it in your loop, and in any case when you did the (pointless) array initialization, root1 was null, as you didn't initialize it.

    0 讨论(0)
提交回复
热议问题