-1

I am trying to reading the info from my XML file (using LinqXML) and using it as variable for a future.

That is a cut from my XML file that I am using currently

<objectgroup name="Object Layer">
  <object id="17" name="A" x="513" y="95" width="510" height="62"/>
  <object id="18" name="B" x="610" y="273" width="476" height="142"/>
  <object id="19" name="C" x="609" y="432" width="320" height="175"/>
  <object id="20" name="D" x="254" y="160" width="33" height="32"/>
  <object id="21" name="E" x="244" y="160" width="33" height="32"/>
</objectgroup>

Now I am trying to get info from it...

public void ObjectCollision()
{
    var collisionObjects = from q in xmlDoc.Descendants("object")
        select new
        {
            id = (int) q.Attribute("id"),
            xCoordinates = (int) q.Attribute("x"),
            yCoordinates = (int) q.Attribute("y"),
            width = (int) q.Attribute("width"),
            height = (int) q.Attribute("height")
        };

And for now, I want to use this information as my variables :) At this point, I want to use my variables as coordinates for Drawing.

foreach (var cobj in collisionObjects)
{
    invisTexture = new Texture("Resources/Map/NotVisible.png");
    collisionSprite = new Sprite(invisTexture);
    CollisionRect.TextureRect = new IntRect(cobj.xCoordinates,  cobj.yCoordinates, cobj.width, cobj.height); 
    collisionSprite.TextureRect = CollisionRect.TextureRect;
}

But whenever I try to start, I am getting a System.NullReferenceException error ^^..

Debugger showed me, that my cobj actually has the right info in it and simple Console.WriteLine works perfectly with those variables.

So, what am I doing wrong?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Suikoden
  • 33
  • 3

1 Answers1

0

Ah..i've forgot about CollisionRect = new RectangleShape(); ^^'' It's time to sleep i think..Anyway, thank you for your help :)

Suikoden
  • 33
  • 3
  • Good time to learn how to use the debugger and execute your code step by step and inspect the variables... – L.B Dec 03 '16 at 22:06