简体   繁体   English

Microsoft.Xna.Framework.Graphics.dll中发生了类型为'System.ArgumentNullException'的未处理异常

[英]An unhandled exception of type 'System.ArgumentNullException' occurred in Microsoft.Xna.Framework.Graphics.dll

Game.cs Game.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Toast_Engine
{
    public class Game : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        GraphicsDevice device;
        Texture2D background;
        Texture2D player;
        public static Texture2D stone;
        public static Texture2D dirt;
        public static Texture2D grass;
        SpriteBatch spriteBatch;
        public Rectangle playerCollision;
        public Rectangle backgroundRectangle;
        createMap map = new createMap();
        Camera cam = new Camera();
        public Vector2 playerPos;
        int playerHealth = 100;
        int playerHeight = 30;
        int playerWidth = 30;
        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            base.Initialize();

            cam.cameraInit();
            playerPos = new Vector2(0, 0);

        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;
            background = Content.Load<Texture2D>("plain");
            player = Content.Load<Texture2D>("Player_Test");
            grass = Content.Load<Texture2D>("grass");
            dirt = Content.Load<Texture2D>("dirt");
            stone = Content.Load<Texture2D>("stone");
        }
        protected override void UnloadContent()
        {
        }
        protected override void Update(GameTime gameTime)
        {
            //UPDATING CODE
            //THIS TEXT IS HERE TO MAKE THE CODE EASY TO SPOT AMONGST OTHER CODE
            //1234567890QWERTYUIOP

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            keyCheck();
            positionPlayerCollision();
            positionCamera();
            positionBackground();


            base.Update(gameTime);
        }
        private void positionBackground()
        {
            int screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
            int screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            backgroundRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
        }
        public void positionCamera()
        {
            cam.Pos = playerPos;
        }
        private void positionPlayerCollision()
        {
            playerCollision = new Rectangle((int)playerPos.X, (int)playerPos.Y, playerWidth, playerHeight);
        }
        private void drawPlayer()
        {
            spriteBatch.Draw(player, playerCollision, Color.White);
        }
        private void drawBackground()
        {
            spriteBatch.Draw(background, backgroundRectangle, Color.White);
        }
        public void keyCheck()
        {
            KeyboardState keysPressed = Keyboard.GetState();
            if(keysPressed.IsKeyDown(Keys.W))
            {
                playerPos.Y--;
            }
            if (keysPressed.IsKeyDown(Keys.A))
            {
                playerPos.X--;
            }
            if (keysPressed.IsKeyDown(Keys.S))
            {
                playerPos.Y++;
            }
            if (keysPressed.IsKeyDown(Keys.D))
            {
                playerPos.X++;
            }
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            //DRAWING CODE
            //THIS TEXT IS HERE TO MAKE THE CODE EASY TO SPOT AMONGST OTHER CODE
            //1234567890QWERTYUIOP

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(device));
            drawPlayer();
            map.renderMap(spriteBatch);
            drawBackground();
            //Console.WriteLine(cam.Pos);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

createMap.cs createMap.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Toast_Engine
{

    class createMap
    {

        public int amountOfTiles = 4;
        public int mapWidth = 5;
        public int mapHeight = 10;
        public int tileSize = 32;
        private Vector2 test = new Vector2(5, 5);
        public int[,] tileMap = new int[,]
        {
        {1,1,2,0,0,2,0,0,1,3},
        {3,0,0,3,3,0,0,0,3,0},
        {2,2,2,2,2,2,2,2,2,2},
        {2,1,1,1,1,1,1,1,1,2},
        {1,1,1,1,1,1,1,1,1,1}
        };
        public Texture2D[] tiles = new Texture2D[] 
        {
           Game.grass,Game.grass,Game.dirt,Game.stone
        };


        public void renderMap(SpriteBatch spriteBatch)
        {
            Console.WriteLine("Thing actually running");
            for (int x = 0; x < mapWidth; x++)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    Console.WriteLine("X = " + x + ", Y = " + y);
                    Vector2 tilePos = new Vector2((x+1)*tileSize, (y+1)*tileSize);
                    int tileID = tileMap[x, y];
                    if(tileID != 0)
                    {
                        Console.WriteLine("Tile ID of " + x + "," + y + " is " + tileID+" , Texture is "+tiles[tileID]);
                        spriteBatch.Draw (tiles[tileID], tilePos, Color.White);
                    }
                }
            }
        }
    }
}

I am trying to create a simple top down tile based game in Xna but whenever I run the program I get this error: 我试图在Xna中创建一个简单的自上而下的基于图块的游戏,但是每当我运行该程序时,都会出现此错误:

An unhandled exception of type 'System.ArgumentNullException' occurred in Microsoft.Xna.Framework.Graphics.dll Microsoft.Xna.Framework.Graphics.dll中发生了类型为'System.ArgumentNullException'的未处理异常

Additional information: This method does not accept null for this parameter. 附加信息:此方法对此参数不接受null。

Through some trial and error I found out that It was this bit of code that was causing the exception: tiles[tileID] in the line: spriteBatch.Draw (tiles[tileID], tilePos, Color.White);, but I can't figure out what to do. 通过一些试验和错误,我发现正是这部分代码引起了异常:该行中的tile [tileID]:spriteBatch.Draw(tiles [tileID],tilePos,Color.White);但是我可以不知道该怎么办。 I checked out An unhandled exception of type 'System.ArgumentNullException' occurred in MonoGame.Framework.dll , but it hasn't helped me as both tiles[] and tileID are initialized. 我签出了MonoGame.Framework.dll中发生的'System.ArgumentNullException'类型的未处理异常 ,但是由于tile []和tileID都被初始化,所以它没有帮助我。 Any help with this problem would be greatly appreciated, thanks. 谢谢您对这个问题的任何帮助。 :P :P

My code: 我的代码:

You're calling a method which doesn't Null as a parameter, if you don't have a line number and the method at that line we don't really know which method could be causing the Exception 您正在调用的方法不是以Null作为参数,如果您没有行号,并且该行的方法我们真的不知道哪个方法可能导致异常

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(device));

It looks like it might be caused by this Method, can you put a breakpoint there and check the values and post the definition here? 看起来可能是由此方法引起的,您可以在此处放置一个断点并检查值并在此处发布定义吗?

It is because when you create your createMap object, map , the textures (grass, stone, dirt) are null so it populates the array tiles in the map object with null items. 这是因为在创建createMap对象map ,纹理(草,石头,污垢)为空,因此会使用空项目填充地图对象中的阵列tiles Later, when you go to draw them, the 1st param (texture2d) is not allowed to be null so it throws the error. 以后,当您绘制它们时,不允许将第一参数(texture2d)为null,因此会引发错误。

If you try to run the code, it stops at that spriteBatch.Draw line and turns it yellow. 如果您尝试运行代码,它将停止在该spriteBatch.Draw行并将其变为黄色。 Hover your mouse cursor over the tiles[tileID] and under it click the + sign. 将鼠标光标悬停在tiles[tileID]上方,然后在其下单击+号。 you will see 3 null textures there. 您将在那里看到3个空纹理。

But if instead, you were to simply declare map without trying to "new" it, then "new it after loading the textures in the LoadContent() method, it would work fine. 但是,如果相反,您只是声明了map而不尝试“对其进行新操作”,然后在将纹理加载到LoadContent()方法中之后“对它进行更新”,它将可以正常工作。

class game
{

  public static Texture2d grass;

  public createMap map;


protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;
            background = Content.Load<Texture2D>("plain");
            player = Content.Load<Texture2D>("Player_Test");
            grass = Content.Load<Texture2D>("grass");
            dirt = Content.Load<Texture2D>("dirt");
            stone = Content.Load<Texture2D>("stone");

//then create the map
                map = new createMap();
            }
     //rest of class

}

So that would be the quick and easy way to get past your immediate problem. 因此,这将是解决当前问题的快速简便的方法。 However, loading up a project with lots of static objects and classes without constructors usually leads quickly to hard-to-manage code base after a while. 但是,在没有大量构造函数的情况下加载包含大量静态对象和类的项目通常会在一段时间后迅速导致难以管理的代码库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mscorlib.dll中发生了类型为&#39;System.ArgumentNullException&#39;的未处理异常附加信息:路径不能为null - An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll Additional information: Path cannot be null Microsoft.AspNet.Identity.EntityFramework.dll中发生类型为“ System.ArgumentNullException”的异常 - An exception of type 'System.ArgumentNullException' occurred in Microsoft.AspNet.Identity.EntityFramework.dll System.Xml.Linq.dll中发生类型为&#39;System.ArgumentNullException&#39;的未处理异常附加信息:值不能为null - An unhandled exception of type 'System.ArgumentNullException' occurred in System.Xml.Linq.dll Additional information: Value cannot be null mscorlib.dll中发生类型&#39;System.ArgumentNullException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.ArgumentNullException' occurred in mscorlib.dll but was not handled in user code 获得未处理的异常:System.ArgumentNullException - Getting unhandled Exception: System.ArgumentNullException 未处理的异常:System.ArgumentNullException:值不能是 null - Unhandled Exception: System.ArgumentNullException: Value cannot be null 抛出异常:mscorlib.ni.dll 中的“System.ArgumentNullException” - Exception thrown: 'System.ArgumentNullException' in mscorlib.ni.dll Microsoft.Extensions.DependencyInjection.dll 中发生类型为“System.AggregateException”的未处理异常: - An unhandled exception of type 'System.AggregateException' occurred in Microsoft.Extensions.DependencyInjection.dll: Microsoft.Speech.dll中发生类型&#39;System.FormatException&#39;的未处理的异常 - An unhandled exception of type 'System.FormatException' occurred in Microsoft.Speech.dll System.dll中发生了类型为&#39;System.InvalidOperationException&#39;的未处理异常 - An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM