访问Body.Joints的NullReferenceException [英] NullReferenceException accessing Body.Joints

查看:56
本文介绍了访问Body.Joints的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问Body上的Joints属性时偶尔会得到NullReferenceExceptions。获得异常的代码如下:

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
}


当弹出异常对话框并启动调试器时,joints参数为null,但是当返回时stacktrace到方法调用body.Joints属性不为null。这可能是某种竞争条件吗?

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?

推荐答案

你复制了车身框架吗?数据( GetAndRefreshBodyData)来自源,如样本中所示?有了这个,你复制后检查是否为空?

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;
    }
}


这篇关于访问Body.Joints的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆