0

I am trying to obtain X,Y,Z positions of skeletal joints from 2 Kinect sensors. So far I have one simple form with 2 buttons and 6 labels. On click on the first button I would like to show x,y,z positions of right hand in label1,2,3. The same I would like to do with second button, but with second kinect device.

It worked prettz well with one Kinect, but when I added code for the second Kinect NullReferenceException was thrown. Can anyone please tell me what do I have wrong? Thank you.

EDIT:

Watch Window:

sensor.SkeletonStream.Enable(); 'sensor.SkeletonStream' is null

Exception message: Object reference not set to an instance of an object.

CODE

public partial class Form1 : Form
{

     Skeleton[] skeletons;
     Skeleton[] skeletons2;

    public Form1()
    {
        InitializeComponent();
        // First Kinect Device
        KinectSensor sensor = KinectSensor.KinectSensors[0];
        sensor.SkeletonStream.Enable();  // Here is the exception thrown
        sensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(SensorSkeletonFrameReady);
        sensor.Start();

        // Second Kinect Device
        KinectSensor sensor2 = KinectSensor.KinectSensors[1];
        sensor2.SkeletonStream.Enable();
        sensor2.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(SensorSkeletonFrameReady2);
        sensor2.Start();


    }


    private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        skeletons = new Skeleton[0];

        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);
            }
        }

    }

    private void SensorSkeletonFrameReady2(object sender, SkeletonFrameReadyEventArgs e)
    {
        skeletons2 = new Skeleton[0];

        using (SkeletonFrame skeletonFrame2 = e.OpenSkeletonFrame())
        {
            if (skeletonFrame2 != null)
            {
                skeletons2 = new Skeleton[skeletonFrame2.SkeletonArrayLength];
                skeletonFrame2.CopySkeletonDataTo(skeletons2);
            }
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach(Skeleton skeleton in skeletons) 
        {
            // get the joint
            Joint rightHand = skeleton.Joints[JointType.HandRight];

            // get the individual points of the right hand
            double rightX = rightHand.Position.X;
            double rightY = rightHand.Position.Y;
            double rightZ = rightHand.Position.Z;

            label1.Text = rightX.ToString();
            label2.Text = rightY.ToString();
            label3.Text = rightZ.ToString();
        }



    }

    private void button2_Click(object sender, EventArgs e)
    {
        foreach (Skeleton skeleton in skeletons2)
        {
            // get the joint
            Joint rightHand = skeleton.Joints[JointType.HandRight];

            // get the individual points of the right hand
            double rightX = rightHand.Position.X;
            double rightY = rightHand.Position.Y;
            double rightZ = rightHand.Position.Z;

            label4.Text = rightX.ToString();
            label5.Text = rightY.ToString();
            label6.Text = rightZ.ToString();
        }
    }



}
user2179427
  • 331
  • 5
  • 16
  • What about call stack and full exception message? – Samuel Jun 06 '14 at 12:12
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Daniel Kelley Jun 06 '14 at 13:08

1 Answers1

0

This is because your sensor object is null. To fix, you should check if your sensor is null before. This can easily be done like this:

KinectSensor sensor = KinectSensor.KinectSensors[0];
if (sensor != null)
{
    sensor.SkeletonStream.Enable();  // Here is the exception not thrown:)
    sensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(SensorSkeletonFrameReady);
    sensor.Start();
}

The developer toolkit also has excellent examples of how to loop through every available sensor and choose the first non null one.

In C# 6.0, there will be a safe navigation operator for this exact purpose!

sensor?.SkeletonStream.Enable();
Liam McInroy
  • 4,339
  • 5
  • 32
  • 53
  • Unfortunately this does not work. The NullReference Exception remains on the same line. It says that: Object reference not set to an instance of an object. And Watch window: sensor.SkeletonStream.Enable() This expression causes side effects and will not be eval – user2179427 Jun 09 '14 at 06:20
  • @user2179427 So `sensor ` is still null? – Liam McInroy Jun 09 '14 at 13:14
  • yes it is. I found out, that when I disconect the second kinect device the error dissapears. But I have to comment out that piece of code for second device (KinectSensor sensor2 = KinectSensor.KinectSensors[1]; , etc.). And now I do not know how to plug both devices into my PC so it will work. – user2179427 Jun 10 '14 at 09:16
  • @user2179427 Great! You can answer and accept your own question to help people with this same error in the future – Liam McInroy Jun 10 '14 at 17:31