0

I am developing a small RTS in C#(with XNA).

I'm setting each unit "goto" position as the vector they should go to. Everything works fine if I don't compare the two Hashtables, but when I do, I get this "NullReferenceException was unhandled" annoying error.

Here's the piece of code I'm getting the error on:

if ( ((float)unit[(int)selectedunits[I+"ID"] + "posX"] != (float)cgoto[(int)selectedunits[I+"ID"] + "X"]) && ((float)unit[(int)selectedunits[I+"ID"] + "posY"] != (float)cgoto[(int)selectedunits[I+"ID"] + "Y"]) ) 

Hopefully this is specific enough.

7
  • How are unit, selectedunits, and cgoto defined? Commented Dec 21, 2011 at 14:12
  • 1
    Please show the Stack Trace of the NullReferenceException. This should highlight exactly where your null reference is. Commented Dec 21, 2011 at 14:12
  • I'd imagine that selectedunits[I+"ID"] is null. Please step through using Visual Studio and check the value at runtime. Commented Dec 21, 2011 at 14:12
  • I forgot to specify that I only get this error when using "cgoto" Hashtable, if I use any other variable everything works fine. Having a better look on the Stack Trace now. Commented Dec 21, 2011 at 14:14
  • which version of .NET are you using? Commented Dec 21, 2011 at 14:14

2 Answers 2

0

Sadly, there isn't enough information to go on here. One of your reference types you're using in your line of code is null at the time of execution. What you can do is set a breakpoint on that line, and execute in debug mode. When the flow of execution hits that line, you can examine all of the references you're using to see which one is null and then diagnose the why it's null from there.

  1. http://msdn.microsoft.com/en-us/library/system.nullreferenceexception.aspx
  2. http://msdn.microsoft.com/en-us/library/490f96s2.aspx
  3. http://msdn.microsoft.com/en-us/library/9kkx3h3c.aspx
Sign up to request clarification or add additional context in comments.

Comments

0

There are not problems in hashtable values comparision you've provided. There some issues with casting and some general design points. I believe you got such exception when trying to cast null to float/int, to avoid such issues use Hashtable.ContainsKey() method to check whether a given key exist in a hashtable and only then acces it's value. Do not forget checking a value for null before cast. Also you can consider using generic typed IDictionary<string, float> (see MSDN) so all values would be typed and you do not need to cast explicitly, this also would give some perfromance gains since no more boxing for value types like in your example float -> object when store a value and object -> float (unboxing) when retrieve back and cast.

2 Comments

Thanks! Checking if the Hashtable contains the Key first prevents this error, however it looks like acting a little strange but I'm pretty sure it's my code's fault.
Here is an other post regarding hash table comparison stackoverflow.com/a/8532525/485076

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.