簡體   English   中英

接口-引發null異常

[英]interface - null exception thrown

我是這個網站的新手,也是編程的新手。 最近,我一直在嘗試學習新技能,以幫助更好地組織/管理我的代碼,並使其更加高效,可讀性強和包含性強。

好的,我不會對此做太多事情,我遇到的問題是在XNA 3.1中,我正在使用C#express 08。

我有一個名為InputHandler的自包含游戲組件,更新循環在基本循環(Game1)之后進行,到目前為止,該循環僅檢查鍵盤輸入並將結果存儲到KeyboardState實例中-該實例具有Get屬性,唯一的其他代碼確實是如果按了退出鍵,它將退出Game1,並在存儲輸入后進行檢查。

碼:

        private KeyboardState keyboardstate;
        public KeyboardState Keyboard_State
        {
            get { return (keyboardstate); }
        }

        public override void Update(GameTime gameTime)
        {
            keyboardstate = Keyboard.GetState();
            if (keyboardstate.IsKeyDown(Keys.Escape))
                Game.Exit();

            base.Update(gameTime);
        }

解決問題,另一個名為Camera的游戲組件嘗試通過IInputHandler的實例(這是接口btw)訪問InputHandler的Keyboard_State屬性。

    public interface IInputHandler
    {
        KeyboardState Keyboard_State { get; }
    }

不用說,此接口是在InputHandler組件內實現的。 移至該錯誤,在我的Camera組件內的更新循環中,有一些邏輯代碼,該邏輯代碼嘗試通過該接口訪問Keyboard_State屬性,檢查某些條件,然后適當地更改攝像機。

            private IInputHandler input;

以下代碼在Camera組件的void更新循環內。

            if (input.Keyboard_State !=null)
            {
                if (input.Keyboard_State.IsKeyDown(Keys.Left))
                    cameraYaw += spinRate;
                if (input.Keyboard_State.IsKeyDown(Keys.Right))
                    cameraYaw -= spinRate;

                if (cameraYaw > 360)
                    cameraYaw -= 360;
                else if (cameraYaw < 360)
                    cameraYaw += 360;
            }

我在* if(input.Keyboard_State!= null)*行收到Null引用異常,抱怨它不是實例。

我是界面的新手,在過去我從未嘗試過使用它們,直到我開始嘗試學習XNA並開始學習有關組件的知識為止,最終我想創建用於管理3D游戲的基本組件(花哨的,只是井井有條,易於管理的)。

任何幫助,將不勝感激。 謝謝 :)

*其他信息*

我的相機構造器是:

        public Camera(Game game)
            : base(game)
        {
            graphics = (GraphicsDeviceManager)Game.Services.GetService(typeof(IGraphicsDeviceManager));
            input = (IInputHandler)game.Services.GetService(typeof(IInputHandler));
        }

而我的InputHandler構造函數為空,我的Game1構造函數為:

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            camera = new Camera(this);
            Components.Add(camera);

            input = new InputHandler(this);
            Components.Add(input);

            input.UpdateOrder = 0;
            camera.UpdateOrder = 1;

            // this component alows Asyncroniously save/load game.
            Components.Add(new GamerServicesComponent(this));

#if DEBUG
            fps = new FPS(this);
            Components.Add(fps);
            fps.UpdateOrder = 1;
            camera.UpdateOrder = 2;
#endif

        }

input是Input handler游戲組件的一個實例。

         private InputHandler input;

希望這可以幫助 :)

在我看來,您沒有在相機中的任何地方初始化“輸入”變量(= input為null)。

因此, if (input.Keyboard_State !=null) -line拋出NullReferenceException(而且,KeyboardState是一個結構,因此它不能為null)。 您說InputHandler和Camera都是游戲組件嗎? 然后嘗試執行以下操作:

在InputHandler構造函數中:

public InputHandler(...)
{
    // Your initialization code here

    this.Game.Services.AddService(typeof(IInputHandler), this);
}

在Camera構造函數中:

public Camera(...)
{
    // Your initialization code here

    input = this.Game.Services.GetService(typeof(IInputHandler)) as IInputHandler;
}

編輯,更新的代碼:

將您的游戲構造函數更改為此:

  public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        input = new InputHandler(this);
        Components.Add(input);
        Services.AddService(typeof(IInputHandler), input);

        camera = new Camera(this);
        Components.Add(camera);



        input.UpdateOrder = 0;
        camera.UpdateOrder = 1;

        // this component alows Asyncroniously save/load game.
        Components.Add(new GamerServicesComponent(this));

#if DEBUG
        fps = new FPS(this);
        Components.Add(fps);
        fps.UpdateOrder = 1;
        camera.UpdateOrder = 2;
#endif

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM