简体   繁体   中英

XNA NullReference Exception, when loading a Texture in Array

Alright I'm stumped here, I can usually figure out most bugs, through use of Google or common sense but this one has me stumped!

I'm trying to load textures to an array of enemies (Like a row in space invaders) within my struct, however when I go to debug it throws a Null Reference, I know this means that I'm trying to access something that isn't there, I believe it's because i am calling this:

Inside Initialize I call my method:

Targets.setupSprite(this);

This is the actual method from inside the struct:

  public void setupSprite(Game1 g)
        {

            widthFactor = 0.05f;
            rowSize = 15;
            spriteSpacingY = spriteRectangle.Height + 5;

            for (int i = 0; i < rowSize; i++)
            {
                if (i < 15)
                    spriteSpacing = 10;



                sprites[i].spriteTexture = g.texture;


                x = 0 + (i * spriteSpacing);
                y = spriteSpacingY;
                visible = true;

            }


        }

Before the Textures can be setup by Content Loader. It recommends using ' new ' but I'm not sure how to use it here.

The line that throws the error as you may have guessed is:

sprites[i].spriteTexture = g.texture;

g.texture is the texture for the enemy and is located in load content.

Any help would be greatly appreciated!

Additional Code:

 protected override void Initialize()
    {
        displayHeight = GraphicsDevice.Viewport.Height;
        displayWidth = GraphicsDevice.Viewport.Width;

        new TargetRowStruct();

            BreadBat.setupSprite(this);
            CheeseBall.setupSprite(this);
            Targets.setupSprite(this);

        base.Initialize();
    }
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        texture = this.Content.Load<Texture2D>("Handsome");
        swag = this.Content.Load<Texture2D>("swag");
        back = this.Content.Load<Texture2D>("Backdrop");
        font = this.Content.Load<SpriteFont>("FontA");


        backRect.Width = (int)displayWidth;
        backRect.Height = (int)displayHeight;

        BreadBat.loadTexture(this);
        CheeseBall.loadTexture(this);
        Targets.loadTexture(this);

        BreadBat.scaleSprites(this);
        CheeseBall.scaleSprites(this);
        Targets.scaleSprites(this);




    }

Relevant Code within Targets Struct:

    public void scaleSprites(Game1 g)
        {

            for (int i = 0; i < rowSize; i++)
            {

                sprites[i].spriteRectangle.Width = (int)((g.displayWidth * sprites[i].widthFactor) + .25f);
                sprites[i].aspectRatio = (int)(sprites[i].spriteTexture.Width / sprites[i].spriteTexture.Height);
                sprites[i].spriteRectangle.Height = sprites[i].spriteRectangle.Width;
            }
        }

        public void loadTexture(Game1 g)
        {
            for (int i = 0; i < rowSize; i++)
            {                   
                sprites[i].spriteTexture = g.texture;
            }
        }

        public void drawSprite(Game1 g)
        {
            g.spriteBatch.Draw(spriteTexture, spriteRectangle, Color.White);

            for (int i = 0; i < rowSize; i++)
            {
                if (sprites[i].visible)
                {
                    g.spriteBatch.Draw(sprites[i].spriteTexture, sprites[i].spriteRectangle, Color.White);
                }
            }
        }

        public void setupSprite(Game1 g)
        {

            widthFactor = 0.05f;
            rowSize = 15;
            spriteSpacingY = spriteRectangle.Height + 5;

            for (int i = 0; i < rowSize; i++)
            {
                if (i < 15)
                    spriteSpacing = 10;






                x = 0 + (i * spriteSpacing);
                y = spriteSpacingY;
                visible = true;

            }


        }

Ps It should be noted that the bread and cheese struct which are almost identical (except for being one sprite instead of an array) work fine.

I still suspect, that inside Target struct You don't initialize this sprites[] array; So, either at the execution time sprites == null or sprites[i] = null;

As I can see, there are three methods invoked on Targets, one is Targets.setupSprite(this); called inside Initialize.

Targets.loadTexture(this); Targets.scaleSprites(this); => those two are called inside LoadContent.

Now, as msdn states, Initialize method is "Called after the Game and GraphicsDevice are created, but before LoadContent."

If, after removing problematic line (which I see You did in the code You added in edit), NullPointerException isn't thrown in relation to sprites[] array, it means, the array is initialized but somewhere after Targets.setupSprite(this); . If it is thrown, it's probably that You didn't initialize it at all.

The exception is pretty clear, sprites is null. Instantiate it using sprites = new Sprite[100] (or whatever class and size you require) and you're good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM