简体   繁体   English

接口-引发null异常

[英]interface - null exception thrown

im both new to this site and new to programming. 我是这个网站的新手,也是编程的新手。 I've been recently trying to learn new skills to help better organise/manage my code and make it both more efficient, readable and contained. 最近,我一直在尝试学习新技能,以帮助更好地组织/管理我的代码,并使其更加高效,可读性强和包含性强。

Okay well i wont go on too much about that, the problem i'm having is in XNA 3.1, I'm using C# express 08. 好的,我不会对此做太多事情,我遇到的问题是在XNA 3.1中,我正在使用C#express 08。

I have a self contained Game conponent called InputHandler, the update loops after the base loop (Game1) which so far just checks for keyboard input and stores the result into an instance of KeyboardState - which has a Get property, the only other code really is it exits Game1 if Escape key is pressed, which it checks for after storing the input. 我有一个名为InputHandler的自包含游戏组件,更新循环在基本循环(Game1)之后进行,到目前为止,该循环仅检查键盘输入并将结果存储到KeyboardState实例中-该实例具有Get属性,唯一的其他代码确实是如果按了退出键,它将退出Game1,并在存储输入后进行检查。

Code: 码:

        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);
        }

moving onto the problem, another game conponent called Camera tries accessing the Keyboard_State property of the InputHandler via an instance of the IInputHandler (this is an interface btw) 解决问题,另一个名为Camera的游戏组件尝试通过IInputHandler的实例(这是接口btw)访问InputHandler的Keyboard_State属性。

    public interface IInputHandler
    {
        KeyboardState Keyboard_State { get; }
    }

it goes without saying that this interface is implemented within the InputHandler component. 不用说,此接口是在InputHandler组件内实现的。 moving onto the error, well I have in my update loop within the Camera component some logic code, which tries to access the Keyboard_State property through the interface, check against some conditions, then alter the camera apropriatly. 移至该错误,在我的Camera组件内的更新循环中,有一些逻辑代码,该逻辑代码尝试通过该接口访问Keyboard_State属性,检查某些条件,然后适当地更改摄像机。

            private IInputHandler input;

following code is within the void update loop.. within the Camera component. 以下代码在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;
            }

I get a Null reference exception at the *if (input.Keyboard_State !=null)* line, complaining that it's not an instance. 我在* if(input.Keyboard_State!= null)*行收到Null引用异常,抱怨它不是实例。

I'm new with Interfaces, I've never seen much of a use for them in the past until i started to try learn XNA, and began learning about conponents, ultimately i want to create the basic components to manage a 3D game (nothing fancy, just organised and manageable). 我是界面的新手,在过去我从未尝试过使用它们,直到我开始尝试学习XNA并开始学习有关组件的知识为止,最终我想创建用于管理3D游戏的基本组件(花哨的,只是井井有条,易于管理的)。

any help would be appreciated. 任何帮助,将不胜感激。 thanks :) 谢谢 :)

* Other info * *其他信息*

my camera constructer is : 我的相机构造器是:

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

and my InputHandler constructer is empty, my Game1 constructer is: 而我的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 is an instance of the Input handler game component. input是Input handler游戏组件的一个实例。

         private InputHandler input;

hope this helps :) 希望这可以帮助 :)

It seems to me that you are not initializing the "input"-variable in the camera anywhere ( = input is null ). 在我看来,您没有在相机中的任何地方初始化“输入”变量(= input为null)。

Because of that, if (input.Keyboard_State !=null) -line throws NullReferenceException ( and by the way KeyboardState is a struct thus it can't be null). 因此, if (input.Keyboard_State !=null) -line抛出NullReferenceException(而且,KeyboardState是一个结构,因此它不能为null)。 You said that both InputHandler and Camera is a game-component? 您说InputHandler和Camera都是游戏组件吗? Try doing something like this then: 然后尝试执行以下操作:

In the InputHandler constructor: 在InputHandler构造函数中:

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

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

And in the Camera constructor: 在Camera构造函数中:

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

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

EDIT, Updated code: 编辑,更新的代码:

Change your Game-constructor to this: 将您的游戏构造函数更改为此:

  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