Search Unity

Object Reference Error when passing a Dictionary - Help?

Discussion in 'Scripting' started by brmusic, Jan 15, 2014.

  1. brmusic

    brmusic

    Joined:
    Sep 18, 2013
    Posts:
    9
    I am trying to pass a dictionary from one script to another to then write into the dictionary in the second script via a trigger. The initialization worked (I added using System.Collections.Generic;), but when I pass the Dictionary I get an error. Odd thing is that when I do the same thing with a list, I have no problem. Here is the error:

    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2.  
    Here is my initialization of my dictionary and list in the first script called NN_PositionNode:

    Code (csharp):
    1.  
    2. public List<NN_PositionNode> connectedPositionList = new List<NN_PositionNode>();
    3. public Dictionary<int,float> distanceToPositionDic = new Dictionary<int,float>();    
    Here is the Code from my second script:
    Code (csharp):
    1.  
    2. void OnTriggerEnter (Collider other)
    3.     {  
    4.         NN_PositionNode NN_CurrentNode = other.gameObject.GetComponent<NN_PositionNode>();     
    5.        
    6.         MyPositionNode.connectedPositionList.Add(NN_CurrentNode);  
    7.   MyPositionNode.distanceToPositionDic.Add(NN_CurrentNode.myID,Vector3.Distance(this.transform.position,other.transform.position));
    8.             }
    9.        
    Anyone have any thoughts?

    Thanks!
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Need to know which line the error occurred on but my guess is that your "other" collider doesn't have an NN_PositionNode component so your GetComponent call is returning null. Attach the debugger and set a breakpoint there and step through, see what's happening.
     
  3. brmusic

    brmusic

    Joined:
    Sep 18, 2013
    Posts:
    9
    Hmm. Well, what is odd is that I have a list in the NN_PositionNode (the same script as the dictionary) that is passed and updated just fine. It is there in the code I put above. The error is on the dictionary line in the collider, not on the list line, which works fine. I will Log the position node and see what happens. If you have any other ideas, please let me know. Thanks!
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    NN_PositionNode NN_CurrentNode = other.gameObject.GetComponent<NN_PositionNode>();
    This line is not getting a NN_PositionNode, so the currentnode variable is null
    Check what the colliding object is.
     
  5. brmusic

    brmusic

    Joined:
    Sep 18, 2013
    Posts:
    9
    @hpjohn, the line is actually getting a NN_PositionNode because I am able to add to a list from that script. I just can't seem to add to the dictionary. The following line is from that same script and it works fine:

    MyPositionNode.connectedPositionList.Add(NN_CurrentNode);


    I am really at a loss. Why would the dictionary return null while the list does not?
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Show us the whole error. Highlight it in the console and give us the whole stack trace, or just the first 3 or 4 lines if it's a long one.
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You are still not getting my point

    Code (csharp):
    1.     class myClass {
    2.         public int id;
    3.     }
    4.  
    5.     List<myClass> aList = new List<myClass>();
    6.     Dictionary<int, float> aDict = new Dictionary<int, float>();
    7.  
    8.     void Start () {
    9.         myClass newClass = new myClass();   //make or get an instance of myClass
    10.         aList.Add( newClass );              //add to list ##WORKS##
    11.         aDict.Add( newClass.id, 5f );       //get property from instance, and add to dict using it ##WORKS##
    12.  
    13.         myClass anotherNewClass = null;     //try to get an instance that is null
    14.                                             //(eg. via getcomponent on a GO that doesnt have component)
    15.         aList.Add( anotherNewClass );       //add null reference to list ##WORKS##
    16.         aDict.Add( anotherNewClass.id, 5f );//get property from a null reference ##DONT WORK##
    17.     }
    18.  
    when the instace of a class is null, you cannot pull properties from it.
    your other.gameObject.GetComponent<NN_PositionNode>(); is probably not finding a NN_PositionNode, so the reference is null, and properties do not exist
     
  8. brmusic

    brmusic

    Joined:
    Sep 18, 2013
    Posts:
    9
    Angry Penguin:
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object ListConnections.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Neural Net Scripts/ListConnections.cs:45) .
    The line is the dictionary.add line.

    hpjohn: I believe I do understand what you are saying. What is weird is that I can debug.log a dictionary declared in both this script and another script without a problem, but then if I .add to the dictionary, all of a sudden I get a NULL reference:

    This is how I reference the script:

    Code (csharp):
    1. MyPositionNode = MyNodeObject.GetComponent<NN_PositionNode>(); 
    This is in the collider and works:

    Code (csharp):
    1.  
    2.             NN_PositionNode NN_CurrentNode = other.gameObject.GetComponent<NN_PositionNode>();     
    3.             Debug.Log("Node Dic:" + NN_CurrentNode.distanceToPositionDic);
    4.  
    However, if I try to add to the dictionary as below, then I get the error above:

    Code (csharp):
    1. MyPositionNode.distanceToPositionDic.Add(CurrentNode.myID,Vector3.Distance(this.transform.position,other.transform.position));
    This doesn't happen with a list. I might be misunderstanding you, but I don't understand why I can add to a list, but not to a dictionary of the same script. I am also referencing a script from the "other" object, and a script on the parent of this script which is allocated like this:

    Code (csharp):
    1.  
    2. if (MyNodeObject == null)   //if the programmer hasn't set the node object, then this can be done automatically via this script
    3.         {
    4.             try
    5.             {
    6.                 MyNodeObject = this.transform.parent.gameObject;
    7.             }
    8.             catch
    9.             {
    10.                 Debug.LogError("Parent isn't a Node");
    11.             }
    12.         }
    13.  
    This doesn't throw an error either... ugh. I read what you sent me and I am pretty sure I am understanding what you are saying and have done that properly. Why would it not a throw an error when I just log the dictionary, but then log an error when I try to add to it? the distance formula works fine in the list as well.