我的程序抛出异常,我无法弄清楚原因

时间:2018-04-19 14:53:23

标签: c#

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

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

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

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

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

任何人都可以帮我弄清楚可能会抛出异常吗?我很感激你的帮助。

1 个答案:

答案 0 :(得分:0)

generateRandomApple方法中,您有以下if声明:

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

当您填充map时,您不会向每个索引添加内容,因此许多map[xposition, yposition]的值均为null。因此,当您在.Equals()检查中对这些值调用if时,您将获得空指针异常。尝试在调用.Equals()之前执行空检查,或者使用等价运算符。