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