NullReferenceException,我如何处理这个

本文关键字:处理 何处理 NullReferenceException | 更新日期: 2023-09-27 18:19:11

我得到这个错误在c#中,而试图建立一个导入瓷砖图。它应该读取如下格式的文件;

DatMapName!
6|4
SpritePageID:SpriteID:OffsetX:OffsetY|SpritePageID:SpriteID:OffsetX:OffsetY SpritePageID:SpriteID:OffsetX:OffsetY
SpritePageID:SpriteID:OffsetX:OffsetY|SpritePageID:SpriteID:OffsetX:OffsetY SpritePageID:SpriteID:OffsetX:OffsetY

转换成这样的数据结构;

map
    MapTile[MapSizeX, MapSizeY]
        Tile[Sprites on tile]
        - string SpritePageID
        - int SpriteID
        - Vector2 Offset

基本上,它的地图名称在第一行,X/Y大小在第二行,然后它有SpritePageID, spriteID, offsetx, offsetY用:分隔,并且每个贴图的多个精灵用|分隔。空格分隔不同的贴图。

但是,在处理过程中,我得到以下错误;

Error   1   Building content threw NullReferenceException: Object reference not set to an instance of an object.
at LibTilePipelineExtension.LevelProcessor.Process(String input, ContentProcessorContext context) in C:'Projects'C#'XNA1'TileWorld'LibTilePipelineExtension'LevelProcessor.cs:line 45
at Microsoft.Xna.Framework.Content.Pipeline.ContentProcessor`2.Microsoft.Xna.Framework.Content.Pipeline.IContentProcessor.Process(Object input, ContentProcessorContext context)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.BuildAssetWorker(BuildItem item)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.BuildAsset(BuildItem item)
at Microsoft.Xna.Framework.Content.Pipeline.BuildCoordinator.RunTheBuild()
at Microsoft.Xna.Framework.Content.Pipeline.Tasks.BuildContent.RemoteProxy.RunTheBuild(BuildCoordinatorSettings settings, TimestampCache timestampCache, ITaskItem[] sourceAssets, String[]& outputContent, String[]& rebuiltContent, String[]& intermediates, Dictionary`2& dependencyTimestamps, KeyValuePair`2[]& warnings)
C:'Projects'C#'XNA1'TileWorld'TileWorld'TileWorldContent'tileworldtest1.tilemap TileWorld

下面是抛出这个错误的代码:

string[] lines = input.Split(new char[] { ''n' });
// ------
// Clear out commented lines from the array. Turn it to a list, remove indices, turn back to array.
List<string> derp1 = new List<string>(lines);
for (int derp2 = 0; derp2 < derp1.Count; derp2++) {
    if (derp1[derp2][0] == ' ' || derp1[derp2][0] == ';') {
        derp1.RemoveAt(derp2);
        derp2--;
    }
}
lines = derp1.ToArray();
// ------
int rows = Convert.ToInt32(lines[1].Split('|')[0]);
int columns = Convert.ToInt32(lines[1].Split('|')[1]);
MapTile[,] res = new MapTile[rows, columns];
for (int i0 = 2; i0 < (rows + 2); i0++) {
    string[] tiles = lines[i0].Split(' ');
    for (int i1 = 0; i1 < columns; i1++) {
        string[] tileSprites = tiles[i1].Split('|');
-->     res[i0 - 2, i1].sprites = new Tile[tileSprites.Length];   <-- Exception
        for (int i2 = 0; i2 < tileSprites.Length; i2++) {
            string[] spriteData = tileSprites[i2].Split(':');
            res[i0 - 2, i1].sprites[i2] = new Tile(spriteData[0], Convert.ToInt32(spriteData[1]), new Vector2(Convert.ToInt32(spriteData[2]), Convert.ToInt32(spriteData[3])));
        }
    }
}
return new Map(res, lines[0]);

这些是我用来制作这个树的类。我删除了using语句来缩短这个问题。

//Map.cs
namespace LibTile {
    public class Map {
        public MapTile[,] grid { get; set; }
        public string MapName { get; set; }
        public Map(MapTile[,] value) {
            grid = value;
        }
        public Map(MapTile[,] value, string name) {
            grid = value;
            MapName = name;
        }
        public MapTile GetTile(int row, int col) {
            return grid[row, col];
        }
        public int Rows {
            get {
                return grid.GetLength(0);
            }
        }
        public int Columns {
            get {
                return grid.GetLength(1);
            }
        }
    }
}

.

//MapTile.cs
namespace LibTile {
    public class MapTile {
        public Tile[] sprites { get; set; }
        public MapTile(Tile[] value) {
            sprites = value;
        }
    }
}

.

//Tile.cs
namespace LibTile {
    public class Tile {
        public String SpritePageID;
        public int SpriteID;
        public Vector2 Offset;
        public Tile(String spid, int sid, int ox, int oy) {
            SpritePageID = spid;
            SpriteID = sid;
            Offset = new Vector2(ox,oy);
        }
        public Tile(String spid, int sid, Vector2 o) {
            SpritePageID = spid;
            SpriteID = sid;
            Offset = o;
        }
    }
}

NullReferenceException,我如何处理这个

您创建了一个新的MapTile数组,但您从未初始化单个元素。试试这个:

res[i0 - 2, i1] = new MapTile(new Tile[tileSprites.Length]);

这将在数组的每个位置创建一个新的MapTile对象。不这样做会导致空引用异常。我还会考虑将MapTile的构造函数更改为无参数,然后像以前一样设置MapTile.sprites