NullReferenceException: Object reference not set to an instance of an object

Hello

I’m getting that error sometimes (not all the time…?) when I receive UDP data from another software.

NullReferenceException: Object reference not set to an instance of an object
OscQballsVr.Update () (at Assets/Scripts/OscQballsVr.cs:687)

The line getting the error is (here 244):

positionTra = (varVector3Pad)quePositionTrajectoire.Dequeue();

I’m not able to find the error by myself after searched many forums… Could somebody have a look at my code?

This is the script (I tried to remove usefulness code):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ParticlePlayground;
using UnityEngine.VR;

public class OscQballsVr : MonoBehaviour {
    public string RemoteIP = "127.0.0.1"; //127.0.0.1 signifies a local host (if testing locally
    public int SendToPort = 9000; //the port you will be sending from
    public int SendToPortLive = 9000; //the port you will be sending from
    public int ListenerPort = 8000; //the port you will be listening on
    private Osc handler;
    private OSCSender hud;

    public Rigidbody Cubes;
    [HideInInspector]
    public animPastille[] pastillesScript;
    [HideInInspector]
    public GameObject[] pastilles;
    private imagesSequence scriptSequence;
    public Transform pastille;
    public int nbrPastilles;
    public float[,] positions;
    public float[,] rotations;
    public float[,] couleurs;
    [HideInInspector]
    public int[] editionPad;
    public int[,] pitchPad;
    [HideInInspector]
    public int[] activationPad;
    [HideInInspector]
    public float[] bounce;
    [HideInInspector]
    public float[] spring;
    public float tempsTransitionPastilles;

    public Color CouleurQballsIntro;
    public Color CouleurQballs;
    public int Qballs;
    [HideInInspector]
    private int Qballsprev;
    public int incrementFixedUpdate = 0;

    public GameObject[] lumieres;

    public GameObject CAM;

    struct varEnvoiBalle
    {
        internal float posX;
        internal float posY;
        internal float posZ;
        internal float forX;
        internal float forY;
        internal float forZ;
        internal float couR;
        internal float couG;
        internal float couB;
        internal int pitch;
        internal int velocity;
        internal string tag;
        internal int trajectoire;
    }
    private varEnvoiBalle envoiBalleCustom;
    private Queue queBalle = new Queue();

    struct varVector3Pad
    {
        internal float X;
        internal float Y;
        internal float Z;
        internal Vector3 vector;
        internal int pad;
    }
    private varVector3Pad positionPad;
    private varVector3Pad rotationPad;
    private varVector3Pad positionTra;

    private Queue quePositionPad = new Queue();
    private Queue queRotationPad = new Queue();
    private Queue quePositionTrajectoire = new Queue();
    private Queue queRotationTrajectoire = new Queue();
    private Queue queAssigneTrajectoire = new Queue();

    struct varAnimPad
    {
        internal float temps;
        internal int sens;
        internal Vector3 position1;
        internal Vector3 position2;
        internal int pad;
        internal bool actif;
    }
    private Queue queAnimRot=new Queue();
    private Queue queAnimTra=new Queue();
    private varAnimPad animTraTemp;


    int msgtarget;
    private bool modulationBool;
    private string modulation;
    private Color ancienneJR;
    private Color ancienneJC;
    private bool bougeDuPlancher;
    private bool msgPluie;
    private bool inverseGrav;

    public bool OscPads=true;
    private bool[] demarreScene;

    private bool eparpille;

    private PlaygroundParticlesC particles;
    private ManipulatorObjectC[] manipulator;
    public GameObject particules;
    private int manipulatorCount = 0;

    public bool offlineMode = false;

    public bool motionBlurActiver=false;

    void Start() {

        if (!offlineMode) SendToPort = SendToPortLive;

        //initialisation OSC
        UDPPacketIO udp = GetComponent<UDPPacketIO>();
        udp.init(RemoteIP, SendToPort, ListenerPort);
        handler = GetComponent<Osc>();
        handler.init(udp);
        handler.SetAllMessageHandler(AllMessageHandler);

        GameObject obj = GameObject.Find("scripts");
        hud = obj.GetComponent<OSCSender>();

        scriptSequence = obj.GetComponent<imagesSequence>();
        demarreScene = new bool[10];

        particles = particules.GetComponentInChildren<PlaygroundParticlesC>();
        manipulator = new ManipulatorObjectC[16];
        for (int i = 0; i < 16; i++)
        {
            manipulator = PlaygroundC.GetManipulator(i, particles);
            //print (manipulator);
        }

        UnityEngine.VR.InputTracking.Recenter();

        //instantiate les trajectoires

        for (int i=0;i<20;i++)
        {
            var go = new GameObject();
            int ij = i + 1;
            go.name = "trajectoire" + ij;
            go.transform.parent = GameObject.Find("trajectoires").transform;
        }


        //instantiate les pastilles
        positions = new float[nbrPastilles + 1, 3];
        pitchPad = new int[nbrPastilles + 1, 2];
        rotations = new float[nbrPastilles + 1, 3];
        couleurs = new float[nbrPastilles + 1, 3];
        editionPad = new int[nbrPastilles + 1];
        activationPad = new int[nbrPastilles + 1];
        bounce = new float[nbrPastilles + 1];
        spring = new float[nbrPastilles + 1];


        pastilles = new GameObject[nbrPastilles + 1];
        pastillesScript = new animPastille[nbrPastilles + 1];

        float rayon = 60;
        for (int i = 1; i < nbrPastilles + 1; i++) {

            float randomf = Random.Range(0, 360);
            Vector3 pos = new Vector3(Mathf.Cos(randomf * 0.01745f) * rayon, 6, Mathf.Sin(randomf * 0.01745f) * rayon);
            Transform instPastille = (Transform)Instantiate(pastille, pos, Quaternion.identity);
            instPastille.transform.parent = transform;

            pastillesScript = instPastille.GetComponent<animPastille>();
            pastillesScript.numPad = i;
            pastilles = instPastille.gameObject;
            instPastille.tag = "Pastille";
            instPastille.gameObject.name = "pastille-" + i;

        }


    }

    public void AllMessageHandler(OscMessage oscMessage)
    {

        string msgString = Osc.OscMessageToString (oscMessage); //the message and value combined
        string msgAddress = oscMessage.Address; //the message parameters
        //object msgValue = oscMessage.Values [0];
        //Debug.Log (msgString);    //log the message and values coming from OSC

        //FUNCTIONS YOU WANT CALLED WHEN A SPECIFIC MESSAGE IS RECEIVED
        switch (msgAddress)
        {
     

            case "/unity/pad/trajectoire/":
                varAnimPad padTrajectoire = new varAnimPad();
                padTrajectoire.pad = (int)oscMessage.Values[0];
                padTrajectoire.sens = (int)oscMessage.Values[1];
                queAssigneTrajectoire.Enqueue(padTrajectoire);
                break;

           
            case "/unity/trajectoire/position/":
                varVector3Pad msgposT = new varVector3Pad();
                msgposT.pad = (int)oscMessage.Values[0];
                msgposT.vector = new Vector3((float)oscMessage.Values[1], (float)oscMessage.Values[2], (float)oscMessage.Values[3]);

                quePositionTrajectoire.Enqueue(msgposT);
                break;

            case "/unity/trajectoire/rotation/":
                varVector3Pad msgposF = new varVector3Pad();
                msgposF.pad = (int)oscMessage.Values[0];
                msgposF.vector = new Vector3((float)oscMessage.Values[1], (float)oscMessage.Values[2], (float)oscMessage.Values[3]);

                queRotationTrajectoire.Enqueue(msgposF);
                break;
         

        }
    }

    void Update()
    {
        //positions des trajectoires
        if (quePositionTrajectoire.Count > 0)
        {
       

            do
            {
           
                positionTra = (varVector3Pad)quePositionTrajectoire.Dequeue();
                if (positionTra.pad != 0)
                {
                    Vector3 newLoc = positionTra.vector;
                    GameObject traj = GameObject.Find("trajectoire" + positionTra.pad);
                    traj.transform.position = newLoc;
                }
            } while (quePositionTrajectoire.Count != 0);
        }

        //rotations des trajectoires
        if (queRotationTrajectoire.Count > 0)
        {
            varVector3Pad rotationTra = new varVector3Pad();
            do
            {
                rotationTra = (varVector3Pad)queRotationTrajectoire.Dequeue();
                if (rotationTra.pad!=0){
                    Quaternion newRot = Quaternion.identity;
                    newRot.eulerAngles = rotationTra.vector;

                    GameObject traj = GameObject.Find("trajectoire" + rotationTra.pad);
                    traj.transform.rotation = newRot;
                }
            } while (queRotationTrajectoire.Count != 0);
        }
}

I noticed I get the errors on all the lines using the pattern variable=(varType)variable.Dequeue();

Please format your (wall of) code.

here you go ! thanks! it’s better like that!

After thinking about it, the problem must be that the Dequeue function is trying to access that variable while that latter is been added by the other thread…

1 Like

The solution I’ve found is to use the function Lock() to lock the variables when it’s been written and read.