NullReferenceException,我该如何处理?

时间:2012-01-02 21:10:45

标签: c# xna nullreferenceexception

我在尝试为tilemap构建导入器时在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用以下分隔:和每个tile的多个sprite被|分隔。空格划分不同的瓷砖。

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

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

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

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

1 个答案:

答案 0 :(得分:0)

您创建了一个MapTile的新数组,但您永远不会初始化各个元素。试试这个:

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

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