2011-03-16 59 views
1

我想为我的钻石游戏显示一组钻石。一切看起来都很好,但我为粗体(或2 **)放置的行得到'NullReferenceException'错误。该项目称为Descending Diamonds,图像位于图形文件夹中。NullReferenceException

任何人都可以阐明一些事情。

 // Initialize graphics library. 
     // Which graphics set are we using? 
     if (GameForm.ClientRectangle.Height < 480) 
     { 
      // The screen height is insufficient for the large graphics set, 
      // so load the small graphics 
      GameGraphics.Clear(); 
      **GameGraphics.Add("Diamonds", new Bitmap(asm.GetManifestResourceStream("DecendingDiamonds.Graphics.SmallDiamonds.png")));** 
      _diamondWidth = 21; 
      _diamondHeight = 16; 
     } 
     else 
     { 
      // We have enough space to use the large graphics set 
      GameGraphics.Clear(); 
      **GameGraphics.Add("Diamonds", new Bitmap(asm.GetManifestResourceStream("DecendingDiamonds.Graphics.BigDiamonds.png")));** 
      _diamondWidth = 42; 
      _diamondHeight = 32; 
     } 
+0

您是否构建了GameGraphics变量? – 2011-03-16 17:35:18

+0

我认为他确实构建了GameGraphics变量,否则他会在他对同一对象调用Clear方法之前在线上得到该异常。 – Lav 2011-03-16 17:38:27

+0

你可以发布堆栈跟踪吗? – 2011-03-17 09:51:21

回答

0

我设法解决了这个问题。我的代码没有问题。它实际上是图形文件夹中图像的设置。我不得不选择“Build Resource”,而不是默认的“Content”。一旦我这样做了,再也没有错误了。

谢谢大家的帮助。

4

由于GameGraphics.Clear()不弹,我能想到的唯一的事情的是,asm为空。为asm添加空检查和/或正确初始化它。

Assembly asm = Assembly.LoadFrom(@"myDll.dll"); 
if(asm != null) 
{ 
    // Initialize graphics library. 
    // Which graphics set are we using? 
    if (GameForm.ClientRectangle.Height < 480) 
    { 
     // The screen height is insufficient for the large graphics set, 
     // so load the small graphics 
     GameGraphics.Clear(); 
     var resourceFromAsm = asm.GetManifestResourceStream("DecendingDiamonds.Graphics.SmallDiamonds.png"); 
     if(resourceFromAsm != null) 
     { 
     **GameGraphics.Add("Diamonds", new Bitmap(resourceFromAsm));** 
     _diamondWidth = 21; 
     _diamondHeight = 16; 
     } 
    } 
    else 
    { 
     // We have enough space to use the large graphics set 
     GameGraphics.Clear(); 
     var resourceFromAsm = asm.GetManifestResourceStream("DecendingDiamonds.Graphics.SmallDiamonds.png"); 
     if(resourceFromAsm != null) 
     { 
     **GameGraphics.Add("Diamonds", new Bitmap(resourceFromAsm));** 
     _diamondWidth = 42; 
     _diamondHeight = 32; 
     } 
    } 
} 
+0

什么样的对象是asm?可能是它的空或缺少其他属性,需要在进行该调用之前设置。 – Lav 2011-03-16 17:37:22

+0

@Lav我的猜测是'大会' – 2011-03-16 17:39:17

+0

没有工作,谢谢你。 – user662973 2011-03-16 17:45:45

3

要么打破你的代码,看看哪个项目返回null,或设置一个断点,并检查立即窗口/ quickwatch。

例如,哪些返回null?

asm/GameGrapics 
asm.GetManifestResourceStream("DecendingDiamonds.Graphics.SmallDiamonds.png") 
GameGraphics.Add("Diamonds", new Bitmap(asm.GetManifestResourceStream("DecendingDiamonds.Graphics.SmallDiamonds.png"))); 

如果这是第一个,那么 - 你只是明白没有asm或GameGraphics设置任何东西。

如果这是第二个,也许它没有找到该文件或加载时出现问题?

如果这是第三次发生的情况,那么在.Add调用中可能存在内部问题?

+0

我会尝试。 – user662973 2011-03-16 17:46:04

+0

它看起来像asm是空的。这是我从断点中得到的结果:“DecendingDiamonds,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null” – user662973 2011-03-16 18:00:31

+0

实际上,它看起来像是有价值的。 PublicKeyToken为null很好。 – RQDQ 2011-03-16 18:02:08

0

我的猜测是asm变量为null。你需要先初始化它。从上下文中我们可以看到它应该是一个组件。如果我们假设资源流位于主程序集中,则可以尝试在您粘贴的代码之前放置

asm = Assembly.GetExecutingAssembly(); 

+0

谢谢,但那已经在那里了,我应该在帖子中列入。 – user662973 2011-03-16 17:44:06

0

GetManifestResourceStream可以返回null。检查“DecendingDiamonds.Graphics.SmallDiamonds.png”的有效性。请参阅此讨论以获取解决方案。