This project has moved and is read-only. For the latest updates, please go here.

An exception of type 'System.NullReferenceException' occurred in FarseerPhysicsXNA.DLL but was not handled in user code

Topics: User Forum
Jan 14, 2014 at 3:07 PM
Edited Jan 14, 2014 at 3:08 PM
I have two different types of animals(bodies) in my game, wolfs and sheeps.
If a wolf touches a sheep, the wolf eats the sheep and the wolf gets 100 points per sheep. In addition, the sheep body must get disabled and removed, because it should not be possible that more than one wolf can eat the same sheep.
I made two different collision categories:
wolfs: Category.Cat23
sheeps: Category.Cat27
In the wolf class, I check if a wolf collides with a sheep:
 if (fixtureB.CollisionCategories == Category.Cat27)
//I want to disable the sheep's body so that this sheep can't collide with another wolf
    if (fixtureB.Body.Enabled == true)
      fixtureB.Body.Enabled = false;         
But I always get an error message in this line of the Farseer's Contact.cs class if I disable the body:
An exception of type 'System.NullReferenceException' occurred in FarseerPhysicsXNA.DLL but was not handled in user code
 //Reverse the order of the reported fixtures. The first fixture is always the one that the user subscribed to.
 if (FixtureB.OnCollision != null)
     Enabled = FixtureB.OnCollision(FixtureB, FixtureA, this);
What is wrong? Isn't it possible to disable a body during a world step?
How can I solve the problem? I need to disable and remove a sheep's body so it can't collide again with another wolf.
Jan 15, 2014 at 5:10 PM
No, i don't think it's possible because you're disabling the body in the middle of a collision.

I solve the problem by just having a boolean in the class called "IsDead" or something similar that gets set in the collision event and checked in the update statement. If it's dead then remove it.
Jan 16, 2014 at 5:16 PM
Edited Jan 16, 2014 at 5:16 PM
The problem is that I don't know which body(sheep) is dead. I have many wolves and many sheep. I tried it like you said, but if I do it like that I always delete every sheep and not only the one that collided with the wolf. I just want to remove the sheep that collided, and not every single sheep. How can I distinguish the sheep? Is it not possible to distinguish the bodies if they are member of the same collision category?

What should I change?

My wolf class looks like this:
public Wolf(…)
//create the wolf's body
//check for collision:
if (fixtureB.CollisionCategories == Category.Cat27)
//I want to remove the sheep's body so that this sheep can't collide with another wolf
//In addition, this wolf can't collide with another sheep because it has already eaten a sheep
    if ((game1.SheepIsDead == false) && (ThisWolfHasEaten == false))
      game1.SheepIsDead = true;    
     ThisWolfHasEaten = true;
//update the wolf's position
//draw the wolf

My sheep class:
public Sheep(…)
//create the sheep's body
//there is no collision detection in this class, because it's already in the wolf class
//update the sheep's position
//draw the sheep
Jan 16, 2014 at 5:40 PM
You want to have the boolean on the sheep class rather than your main game class. You can store a reference to your sheep class in the Body when you create it in the UserData variable

So in the sheep class after you've created the body you can add

body.UserData = this;

And then in the collision event you can reference it like this (UserData is an Object type so you'll need to cast it to reference any class variables)

((SheepClass)fixtureB.Body.UserData).IsDead = true;

Then in your main update method you can do the check

foreach(SheepClass sheep in MyListOfSheep)
if (sheep.IsDead)
//remove body here
Marked as answer by Wizard999 on 1/18/2014 at 9:56 AM
Jan 18, 2014 at 5:56 PM
Thanx. It works.
Feb 1, 2014 at 5:57 PM
I'm also ran across this issue and I think it'd be much nicer if Farseer could handle it. My solution is here: