Search Unity

Unity throwing Null reference error, I cannot find the reason. Help please.

Discussion in 'Scripting' started by zaqmklp, Oct 2, 2019.

  1. zaqmklp

    zaqmklp

    Joined:
    Sep 24, 2019
    Posts:
    4
    So, I'm trying to create a 3D minimal spanning tree out of 'Node' objects in my program. The node class is not attached to any game object, so it's not a Monobehavior object. To represent the edges in the MST, Every node instance has an arraylist of nodes that are connected to it. It also has a public funtion add(Node n) which allows more nodes to be added to the list.

    Code (CSharp):
    1.  
    2. public class Node{
    3.  
    4. //other stuff here
    5.  
    6. public void add(Node n)
    7.     {
    8.         connections.Add(n);
    9.     }
    10. }
    11.  
    I store a 3d array of nodes in another class, which I use to generate the MST. This other class is attached to an empty object in the game. The node array in this class is first declared globally before start():

    Code (CSharp):
    1.  
    2. public Node[,,] rep = new Node[8, 8, 16];
    3.  
    Then, in start(), I fill it with all of the nodes. Note, nodes are instantiated with position variables:

    Code (CSharp):
    1.  
    2. for (int i = 0; i < 8; i++)
    3.         {
    4.             for (int j = 0; j < 8; j++)
    5.             {
    6.                 for (int k = 0; k < 16; k++)
    7.                 {
    8.                     rep[i, j, k] = new Node(i, j, k);
    9.                 }
    10.             }
    11.         }
    12.  
    After this, start() calls the function that should generate the MST. One section of this function goes as follows:
    (curr is a node as well).

    Code (CSharp):
    1.  
    2. newNode = rep[currX, currZ - 1, currT];
    3.  
    4. frontier.Remove(newNode);
    5. added.Push(newNode);
    6. newNodeExists = true;
    7. newNode.execTest(); //dummy method
    8. curr.execTest(); //dummy method
    9.  
    10. newNode.add(curr);
    11.  
    Unity throws a "NullReferenceException: Object reference not set to an instance of the object" on the "newNode.add(curr)" line, even though both nodes are instantiated. I tried testing for null on both curr, and newNode, and neither of them are. I also tried accessing some of both curr's and newNode's local fields, and printing them to Debug. Both sets of local fields were accurate. For a final test, I created a dummy method in the node class to see if it would execute properly (execTest()), and it did. Overall, I have no idea where the null reference is coming from, because both objects seem very instantiated. So why does execTest() work if add doesn't? And how can I fix this? Any advice is greatly appreciated! Let me know if I should post more code![/code]
     
    Last edited: Oct 2, 2019
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
  3. zaqmklp

    zaqmklp

    Joined:
    Sep 24, 2019
    Posts:
    4
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    since you are calling the execTest on curr and newNode and getting past those lines of code, then the connections List must be null in the node object.
     
    zaqmklp likes this.
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Please use code tags.

    But in General, if you beleive your nodes are 'very instantiated', why not make sure. Insert a Debug.Log("node " + theNode + "exists") before each time you reference a node, and then Output to the console that the object is indeed instantiated.
     
    zaqmklp likes this.
  6. zaqmklp

    zaqmklp

    Joined:
    Sep 24, 2019
    Posts:
    4
    Actually, I just tested, and you're totally right! That's so weird, I initialize the connections arraylist in the start function, but maybe I'll try changing that.

    Edit: Thanks! This solved my problem, I wasn't properly creating the arraylist. I have new errors now, but they aren't related to Null references at least!
     
    Last edited: Oct 2, 2019
  7. zaqmklp

    zaqmklp

    Joined:
    Sep 24, 2019
    Posts:
    4
    Ok, I tried putting the message you said after every reference. It prints only "node Node exists" Though, when I'm referencing curr, should it say "node curr exists?" I'm not sure how the message is supposed to look.
     
    Last edited: Oct 2, 2019