none
NullReferenceException accessing Body.Joints RRS feed

  • Question

  • I'm sporadically getting NullReferenceExceptions when trying to access the Joints property on Body. The piece of code that gets the exception is the following:

    this.DrawBone(body.Joints, JointType.WristRight, JointType.HandRight);
    
    void DrawBone(IReadOnlyDictionary<JointType, Joint> joints, JointType startJoint, JointType endJoint)
    {
        Vector2 start = this.mapMethod(joints[startJoint].Position);
        Vector2 end = this.mapMethod(joints[endJoint].Position);
        Vector2 diff = end - start;
        Vector2 scale = new Vector2(1.0f, diff.Length() / this.boneTexture.Height);
         .. etc
    }
    
    When the exception dialog pops up and the debugger is started the joints argument is null, however when going back up the stacktrace to the method call the body.Joints property is not null. Could this be some kind of race condition?

    Monday, January 19, 2015 3:24 PM

All replies

  • Have you copied the body frame data(GetAndRefreshBodyData) from the source as demonstrated in the samples? With that, after the copy did you check for null?

    private Body[] bodies = null;
    
    using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
    {
        if (bodyFrame != null)
        {
            if (this.bodies == null)
            {
                this.bodies = new Body[bodyFrame.BodyCount];
            }
    
            // The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
            // As long as those body objects are not disposed and not set to null in the array,
            // those body objects will be re-used.
            bodyFrame.GetAndRefreshBodyData(this.bodies);
            dataReceived = true;
        }
    }


    Carmine Sirignano - MSFT

    Monday, January 19, 2015 6:23 PM
  • Polling the body frames and drawing the joints/bones is done on separate threads. Could it be the case that the Kinect SDK is refilling the IReadonlyDictionary<JointType, Joint> on Body.Joints while I'm drawing them on another thread?
    Monday, March 2, 2015 1:20 PM
  • Very well could be, but without seeing your code it could be as simple as checking to see if you initialized the block. If you have copied the bodies they should be valid and not null after the first time.

    Whenever you are using threads, you will need to setup some type of resource lock mechanism to ensure you are not reading from something that is being written to.


    Carmine Sirignano - MSFT


    Monday, March 2, 2015 8:27 PM