2011-03-16 85 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」的有效性。請參閱此討論以獲取解決方案。