2011-03-16 5 views
1

내 다이아몬드 게임에 다이아몬드 세트를 표시하려고합니다. 다 괜찮아 보이지만 굵게 (또는 2 **) 넣어 라인에 대한 'NullReferenceException'오류가 발생합니다. 이 프로젝트는 Descending Diamonds라고하며 이미지는 그래픽 폴더에 있습니다.NullReferenceException

누구나 사물에 대해 밝힐 수 있습니까? GameGraphics.Clear()가 폭격하지 않기 때문에

 // 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 변수를 구성 했습니까? –

+0

나는 그가 GameGraphics 변수를 만들었다 고 생각한다. 그렇지 않으면 그는 동일한 객체에 대해 Clear 메소드를 호출하기 전에 그 라인에서 예외를 얻었을 것이다. – Lav

+0

스택 추적을 게시 할 수 있습니까? –

답변

0

문제를 해결할 수있었습니다. 내 코드에는 아무런 문제가 없었습니다. 그것은 실제로 그래픽 폴더의 이미지 설정입니다. 기본 "컨텐츠"대신 "임베디드 리소스"를 선택한 "빌드 동작"을 선택해야했습니다. 일단 내가 한 후에 더 이상 오류가 없었습니다.

감사합니다. 모두 도와 주셔서 감사합니다.

4

는, 내가 생각할 수있는 유일한 방법이며, asm은 null입니다. asm에 대해 null 체크를 추가하거나 적절하게 초기화하십시오.

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입니까? 호출하기 전에 설정해야하는 null 또는 누락 된 다른 속성 일 수 있습니다. – Lav

+0

@Lav 내 추측은'Assembly'입니다. –

+0

작동하지 않았습니다. 감사합니다. – user662973

3

어떤 항목이 null을 반환하는지 확인하거나 중단 점을 설정하고 직접 실행 창/빠른보기에서 검사하십시오.

예를 들어, 이들 중 어느 것이 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

+0

asm이 null 인 것처럼 보입니다. 이것은 중단 점을 사용하여 얻은 것입니다. "DecendingDiamonds, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null" – user662973

+0

사실, 값이있는 것처럼 보입니다. PublicKeyToken이 null이면 좋습니다. – RQDQ

0

내 생각 엔 asm 변수가 null입니다. 먼저 초기화해야합니다. 문맥에서 우리는 어셈블리가되어야한다고 봅니다. 리소스 스트림이 주 어셈블리에 있다고 가정하면 붙인 코드 앞에

asm = Assembly.GetExecutingAssembly(); 

을 넣으십시오.

+0

고마워,하지만 이미 그곳에 있었다. – user662973

0

GetManifestResourceStream은 null을 반환 할 수 있습니다. "DecendingDiamonds.Graphics.SmallDiamonds.png"의 유효성을 확인하십시오. 해결책은이 토론을 참조하십시오.

관련 문제