我的程序抛出异常,我不知道为什么



我正在尝试创建一个贪吃蛇游戏,但我的代码抛出异常,我无法弄清楚它可能是什么。

我正在创建这个贪吃蛇游戏,因为我想学习更多的 c#,因为我的老师说第 4 季度我可以选择我想用我的时间做什么。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace snake_game
{
    class Program
    {
        static void Main(string[] args)
        {
            int xposition = 26;
            int yposition = 26;
            int LeftColumn = 1;
            int rightcolumn = 50;
            int topcolumn = 1;
            int bottomcolumn = 50;
            string[,] map = new string[51, 51];
            map = buildWall(LeftColumn, rightcolumn, topcolumn, 
            bottomcolumn, map);
            //places down the player and updates the map to tell where you are
            Console.SetCursorPosition(xposition, yposition);
            Console.Write((char)2);
            map[xposition, yposition] = "player";
            map = generateRandomApple(map, LeftColumn, rightcolumn, 
            topcolumn, bottomcolumn);
            placeApple(map);
            Console.ReadKey();
        }
        private static void placeApple(string[,] map)
        {
            //places down the apple
            for (int x = 0; x < map.Length; x++)
        {
            for (int y = 0; y < map.Length; y++)
            {
                if (map[x, y].Equals("apple"))
                {
                    Console.Write("a");
                    break;
                }
            }
        }
    }
    private static string[,] generateRandomApple(string[,] map, int lc, int 
    rc, int tc, int bc)
    {
        Random rnd = new Random();
        int xposition;
        int yposition;
        while (true)
        {
            //generates random cordinates to place down the apple
            xposition = rnd.Next(lc, rc);
            yposition = rnd.Next(tc, bc);
            //sets the property that the apple wants to be at to the apple 
            if it isnt open in the map
            if ((!map[xposition, yposition].Equals("the wall")) && 
            (!map[xposition, yposition].Equals("player")))
            {
                map[xposition, yposition] = "apple";
                break;
            }
        }
        return map;
    }
    private static string[,] buildWall(int leftcolumn, int rightcolumn, int 
    topcolumn, int bottomcolumn, string[,] map)
    {
        //generates the left and right walls
        for (int i = leftcolumn; i <= rightcolumn ; i++)
        {
            Console.SetCursorPosition(leftcolumn, i);
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("#");
            map[leftcolumn, i] = "the wall";
            Console.SetCursorPosition(rightcolumn, i);
            Console.Write("#");
            map[rightcolumn, i] = "the wall";
        }
        //generates the top and bottom walls
        for (int i = topcolumn; i <= bottomcolumn; i++)
        {
            Console.SetCursorPosition(i, topcolumn);
            Console.Write("#");
            map[i, topcolumn] = "the wall";
            Console.SetCursorPosition(i, bottomcolumn);
            Console.Write("#");
            map[i, bottomcolumn] = "the wall";
        }
        return map;
     }
   }
 }

它应该设置地图,但函数中的 if 语句getRandomApple抛出异常,特别是检查该地点是否有玩家的部分,它抛出的异常是说

NullReferenEexception was being unhandled(object reference not set to an 
instance of an object).

谁能帮我弄清楚什么可能会引发异常?我将不胜感激。

generateRandomApple 方法中,您有以下 if 语句:

if ((!map[xposition, yposition].Equals("the wall")) && (!map[xposition, yposition].Equals("player")))

当你填充你的map时,你不会向每个索引添加一些东西,所以许多map[xposition, yposition]的值都是null的。因此,当您在if检查中对这些值调用.Equals()时,您会收到空指针异常。尝试在调用 .Equals() 之前执行空检查,或改用等价运算符。

相关内容