Image first loaded, then it isn't? (XNA)

Posted by M0rgenstern on Game Development See other posts from Game Development or by M0rgenstern
Published on 2011-11-15T15:03:00Z Indexed on 2011/11/15 18:09 UTC
Read the original article Hit count: 244

Filed under:
|
|

I am very confused at the Moment.

I have the following Class: (Just a part of the class):

 public class GUIWindow
{
    #region Static Fields
    //The standard image for windows.
    public static IngameImage StandardBackgroundImage;
    #endregion
}

IngameImage is just one of my own classes, but actually it contains a Texture2D (and some other things). In another class I load a list of GUIButtons by deserializing a XML file.

public static GUI Initializazion(string pXMLPath, ContentManager pConMan)
    {
        GUI myGUI = pConMan.Load<GUI>(pXMLPath);
        GUIWindow.StandardBackgroundImage = new   
        IngameImage(pConMan.Load<Texture2D>(myGUI.WindowStandardBackgroundImagePath),
                 Vector2.Zero, 1024, 600, 1, 0, Color.White, 1.0f,
                 true, false, false);
        System.Console.WriteLine("Image loaded? " +  
                                (GUIWindow.StandardBackgroundImage.ImageStrip != null));
        myGUI.Windows = pConMan.Load<List<GUIWindow>>(myGUI.GUIFormatXMLPath);
        System.Console.WriteLine("Windows loaded");

        return myGUI;
    }

Here this line: System.Console.WriteLine("Image loaded? " +
(GUIWindow.StandardBackgroundImage.ImageStrip != null)); Prints "true". To load the GUIWindows I need an "empty" constructor, which looks like that:

public GUIWindow()
    {
        Name = "";
        Buttons = new List<Button>();
        ImagePath = "";
        System.Console.WriteLine("Image loaded? (In win) " + 
                                (GUIWindow.StandardBackgroundImage.ImageStrip != null));
        //Image = new IngameImage(StandardBackgroundImage);
        //System.Console.WriteLine(
        //Image.IsActive = false;
        SelectedButton = null;
        IsActive = false;
    }

As you can see, I commented lines out in the constructor. Because: Otherwise this would crash. Here the line System.Console.WriteLine("Image loaded? (In win) " + (GUIWindow.StandardBackgroundImage.ImageStrip != null)); Doesn't print anything, it just crashes with the following errormessage:

Building content threw NullReferenceException: Object reference not set to an object instance.

Why does this happen? Before the program wants to load the List, it prints "true". But in the constructor, so in the loading of the list it prints "false". Can anybody please tell me why this happens and how to fix it?

© Game Development or respective owner

Related posts about XNA

Related posts about c#