-4

This code:

public void collide(int x, int y)
{
   int foodthen=0;
   for(int xt=0;xt<150;xt++)
   {
      for(int  yt=0;yt<55;yt++)
      {
~        if(MainClass.tilesSet[yt,xt].food=true)
         {
            foodthen++;
         }
      }
   }
   Debug.WriteLine("Food then: "+foodthen);
   if(this.Equals(MainClass.fridge)||this.Equals(MainClass.tree))
   {
      if(MainClass.tilesSet[y,x].food)
      {
         MainClass.tilesSet[y,x].food=false;
         MainClass.Log("You found some food!");
         MainClass.player.food++;
      }

      else
      {
         MainClass.Log("There is no food... :(");
      }
    }
    MainClass.player.updateFood();
 }

This throws a NullReferenceException at the line marked with a '~'. The same code as in the line marked with a '#' seems to be okay, no exception thrown when I test it without the throwing code. Why does it only happen when in a for loop?

Brian
  • 5,069
  • 7
  • 37
  • 47
Hobbit9797
  • 73
  • 5
  • You are apparently referencing a location out of bounds? I don't know.. code looks strange to me. –  Apr 16 '13 at 16:41
  • 1
    I'm probably going blind. Where's the line marked with a #? – Melanie Apr 16 '13 at 16:41
  • what syntax is that ? – karthikr Apr 16 '13 at 16:41
  • What are `xt` and `yt` when the exception occurs? – torrential coding Apr 16 '13 at 16:41
  • 2
    There isn't enough to really figure out what is going on. My best guess is as you're looping maybe you have your array dimensioned appropriately to avoid the out of range exception, but the objects within are null. Since x and y are passed in as parameters, maybe at that position the object is not null. Either way, debug it. Your debugger should break automatically when the exception is thrown. Then investigate your array and see if there is a null object at YT,XT. – Josh Apr 16 '13 at 16:41
  • @Melanie look for `~` not `#` – karthikr Apr 16 '13 at 16:41
  • @karthikr: "The same code as in the line marked with a # " –  Apr 16 '13 at 16:43
  • 1
    one part of `MainClass.tilesSet[yt,xt].food` is null. Start building it up part by part to find out where. (eg `var xx = MainClass`, put breakpoint, inspect; `var yy = MainClass.tilesSet', put breakpoint, inspect, etc) – Jonathan Apr 16 '13 at 16:43
  • @Hobbit9797 - Your code is incomplete. There is no line marked with # which is important. If you are getting a null exception it means the object your trying to reference is null so implement code to prevent that from happening. – Security Hound Apr 16 '13 at 18:50
  • 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) – Alberto Mar 21 '14 at 10:18

3 Answers3

4

Either MainClass, MainClass.tilesSet or MainClass.tilesSet[yt,xt] is null;

Use the visual studio debugger to put a break-point on that line to see which one it is

2

This means that MainClass, MainClass.tilesSet or MainClass.tilesSet[yt,xt] is null. Assuming MainClass is actually a class, it cannot be null, so most likely one of the other two. My best guess is that the multi-dimensional array was not completely initialized and this contains some null references. Use the debugger to inspect the value of yt and xt when the exception occurs.

If yt and xt are both 0, MainClass.tilesSet or MainClass.tilesSet[0,0] is null. For any other value, it's a problem with the array not being completely filled.

Also, please note that in that line of code you're assigning true to the property (or field) food, not checking against true. This is most probably not what you want.

if(MainClass.tilesSet[yt,xt].food=true)

Should be:

if(MainClass.tilesSet[yt,xt].food)
Thorarin
  • 47,289
  • 11
  • 75
  • 111
0

If you're getting a NullReferenceException then that would indicate tilesSet array hasn't been initialized. If the loop was referencing an index that was out of scope you'd get an exception for that, but the exception you're getting is telling you that you're asking for an array object that doesn't exist.

I would look at how that property of the MainClass is initialized within the context of when the collide method is called.

Charles Wesley
  • 828
  • 12
  • 27