-1

I am currently trying to create a game of battleship using the kinect sensor in c# WinForms. I have gotten all the basics and an have been trying to implement a class in my project that will convert the skeleton frame data into something that I can use to control the mouse with my had. i have been following a bit of sample code that I have found online, but have run into a big issue with Stopwatches. With the code I have, i am calling

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Coding4Fun.Kinect.Wpf;
using Microsoft.Kinect;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;
using System.Windows;

namespace TCP_Server
{
   class motionClass
   {
      //other objects are declared in here as well.
      private readonly Stopwatch _clickHoldingTimer;

and in my error output window, I get the following:

Field 'TCP_Server.motionClass._clickHoldingTimer' is never assigned to, and will always have its default value null

the purpose of the stopwatch is to determine how long a user has made a grabbing motion and preform a mouse click if its been held for a certain amount of time.

if I run the program to the point where my motionClass is initialized, as soon as it sees a skeleton in the frame that is being tracked and runs into the first call for the stopwatch, I get thrown a nullValueException.

why is a stopwatch throwing this exception, and is there any way to fix it? I can provide more code and show where the exception is thrown if needed.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Termonator145
  • 109
  • 1
  • 14
  • show the code where you use the stopWatch... we can only guess with what you've shown. – T McKeown Apr 21 '15 at 00:19
  • 2
    What part of the warning don't you understand? – SLaks Apr 21 '15 at 00:19
  • I guess you don't initialize StopWatch object any where. just make sure you create new instance in constructor – Peyman Apr 21 '15 at 00:19
  • "What is NRE and how to fix it" seem to cover your case based on amount of code shown. If it does not provide enough information make sure to update your question so it is clear how your problem is different from once described in the [linked question](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). – Alexei Levenkov Apr 21 '15 at 00:52

2 Answers2

1

I had tried changing my code to

private readonly Stopwatch _clickHoldingTimer = new Stopwatch();

but it still threw the errors at me. What I just tried was closing and restarting Visual Studio, using this code instead of what I had originally, and it fixed itself.

Termonator145
  • 109
  • 1
  • 14
  • You've now "new'd" the stopwatch; but you still need to actually use it. read up on MSDN on the methods and properties of stopwatch to see how you can use it. Ie, in your first post you had the idea of a stopwatch but didn't have one; Now you've got one in your pocket, but what you really want to do is use it – andrew Apr 21 '15 at 00:37
0

Your variable is apparently declared but never "new'd", declaring a variable simply defines it's type but the variable isn't an object until it is instantiated. If the code you've shown is the only place where you use this variable then remove the declaration as it's not needed/used.

In order to use your stop watch object you'd need to have this line execute (or something similar):

_clickHoldingTimer = new StopWatch();
T McKeown
  • 12,971
  • 1
  • 25
  • 32