我想學習MonoGame,我正在嘗試做基本的東西,我已經想到了我自己的大部分,但我得到一個空指針,我不知道爲什麼。我試圖讓MainMenuScene只繪製紋理,但在SceneManager中currentScene不斷給我空指針,我不知道爲什麼。我張貼我的SceneManager代碼,但其餘的將在github上。它沒有太多的代碼,所以它不應該花很長時間,如果你看看它。我感到非常困惑,我不知道該怎麼去谷歌試圖弄清楚自己。即使我使用變量獲得null錯誤
https://github.com/eatmykhack/MonoGame.git
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tutorial.Scenes;
class SceneManager
{
private static SceneManager instance = new SceneManager();
Scene currentScene, newScene;
Dictionary<string, Scene> sceneDirectory =new Dictionary<string, Scene>();
public static SceneManager getInstance()
{
return instance;
}
public static Scene getCurrentScene()
{
return instance.currentScene;
}
public static void changeScene(Scene scene)
{
}
public static void addScene(string sceneName)
{
instance.newScene = instance.sceneDirectory[sceneName];
}
public void update() { currentScene.Update(); }
public void draw(SpriteBatch spriteBatch) { currentScene.Draw(spriteBatch); }
public void Initialize()
{
instance.sceneDirectory["MainMenuScene"] = new MainMenuScene();
instance.currentScene = instance.sceneDirectory["MainMenuScene"];
}
public void LoadContent(ContentManager content)
{
instance.currentScene.LoadContent(content);
}
public void UnloadContent()
{
instance.currentScene.UnloadContent();
}
}
的可能的複製[什麼是一個NullReferenceException,如何解決呢?(http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i -fix-it) – Viru
我知道NullReferenceException是什麼,但我的問題是我正在使用有問題的變量,但它仍然給我它。 – user4648142
你是什麼意思我使用變量?如何使用變量保證你不會得到空引用異常?無論如何,如果你發佈堆棧跟蹤,那麼你會得到一些代碼的哪一部分可能失敗的指針....如果你可以調試,那麼你甚至可以確定代碼行失敗....但我沒有在你的問題中看到任何這樣的信息 – Viru