2011-11-15 70 views
0

我当时非常困惑。图片先加载,那不是? (XNA)

我有以下类别:(类的只是一部分):

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

IngameImage只是我自己的一个类,但实际上它包含一个Texture2D(和其他一些东西)。 在另一个类中,我通过反序列化XML文件来加载GUIButton的列表。

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; 
    } 

在这里这条线:的System.Console.WriteLine( “图像加载?” +
(GUIWindow.StandardBackgroundImage.ImageStrip = NULL)!); 打印“true”。 要加载GUIWindows我需要一个“空”构造函数,它看起来像这样:

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; 
    } 

正如你所看到的,我在构造函数中注释行了。因为:否则这会崩溃。 这里行System.Console.WriteLine(“图片加载?(赢))”+ (GUIWindow.StandardBackgroundImage.ImageStrip!= null)); 不打印任何东西,它只是崩溃与下面的ErrorMessage:

建设内容扔的NullReferenceException:未将对象引用设置到对象的实例。

为什么会发生这种情况? 在程序想要加载列表之前,它会打印“true”。但是在构造函数中,所以在加载列表时打印出“false”。 任何人都可以告诉我为什么发生这种情况,以及如何解决它?

+5

使用你的调试器。 NullReferenceException意味着你试图用一个设置为null的变量来做某件事。 – doppelgreener

+0

您是否使用MSDN的[游戏状态管理](http://create.msdn.com/en-US/education/catalog/sample/game_state_management)代码? –

+0

在构建内容时抛出异常,显然,在运行应用程序时应该是一个线索。为什么在构建内容期间运行此代码?你想将哪些内容编译成可能导致它运行的内容? – 2011-11-16 01:00:12

回答

2

我在NullReferenceException中的最佳猜测是GUIWindow.StandardBackgroundImage为空,因此当您尝试访问GUIWindow.StandardBackgroundImage.ImageStrip时会引发此异常。

您是否熟悉Visual Studio调试器?如果不是,你应该是。我会设置一些断点,并通过任何代码读取或写入StandardBackgroundImage

真的,你的组织可以改进。为什么StandardBackgroundImageGUIWindow类的静态字段?它应该是加载它的类的字段 - 无论Initialization方法在哪里。然后将它传递给GUIWindow的构造函数。

您正在将StandardBackgroundImage字段视为全局字段,因此会感受到该决定的影响 - 有些内容正在读取和修改它,并且您无法跟踪它们正在执行的操作顺序。

Take this advice on globals.

+0

那么,问题不是GUIWindow.StandardBackgroundImage。这两种情况都不为空。这个问题真的好像是保存在StandardBackgroundImage.ImageStrip中的Texture2D。在第一个检查中它不是null,那么在构造函数中它是空的,并且在所有事物之后它不为null。它在构造函数中仅为null。由于其他:不要担心。每个窗口都有一个图像属性,它拥有自己的图像。全局的StandardBackgroundImage只是给窗口一个标准的图像,如果它不加载任何特殊的图像。 – M0rgenstern

+0

好吧,既然上面是错误的,显然你想保持全球...这是另一个想法:我相信ContentManager使用一个单独的线程。也许静态的'StandardBackgroundImage'尚未复制到该线程。如果出现这种情况,可以使用['volatile'关键字] [2]对其进行补救。试一试,看看它是否修复。 – Ricket

+0

它似乎并不是因为它使用不同的线程。我使用了volatile关键字,它抛出了和以前一样的错误。我不想保持全球化,我会改变这种情况,如果我可以考虑任何其他解决方案,但我真的看不到解决方案。 – M0rgenstern