0

I am trying to create a maze game, I'm using arrays to house my maze and while it was working fine a couple of test runs ago, VS is now giving me a null reference exception. I don't know how my array is null, and I could use all the help I can get. The specific place I get the null reference exception is in my DisplayMaze method. I decided to just give all of my code in case that helps.

  class Maze
    {
        private string[] mazeMap;
        private int step = 0;
        private int playerx =1;
        private int playery =1;
        public int GetPlayerX()
        {
            return playerx;
        }
        public int GetPlayerY()
        {
            return playery;
        }
        public void SetPlayerX(int x)
        {
            playerx = x;
        }
        public void SetPlayerY(int y)
        {
            playery = y;
        }
        public void SetMaze(string[] map)
        {
            mazeMap = map;
        }
        public string[] GetMaze()
        {
            return mazeMap;
        }
        public int[] PlayerLocation()
        {
             int[] location = new int[] { playerx, playery };
            return location;
            
        }
        public void DisplayMaze()
        {
            string[] map = GetMaze();
            for (int x = 0; x < map.Length; x++)
            {
                Console.WriteLine(map[x]);
                if (map[x] == map[playerx] && map[x] == map[playery])
                {
                    Console.WriteLine("O");
                }     
            }   
        }
      
        public void QuitGame()
        {
            string answer;
            Console.WriteLine("Are you sure you want to quit? Type Y - Yes or N - No");
            answer = Console.ReadLine();
            if (answer == "Y")
            {
                System.Environment.Exit(1);
            }

            else if (answer == "N")
            {
                
            }
            else
                Console.WriteLine("Please enter Y or N");
        }
        public void TitleScreen()
        {
            string[] map = title;
            for (int x = 0; x < map.Length; x++)
            {
                Console.WriteLine(map[x]);
            }
            Console.WriteLine("Press ENTER to start");
            Console.WriteLine("Quit");
            ConsoleKey ckj;
            ckj = Console.ReadKey(true).Key;
            switch (ckj)

            {
                case ConsoleKey.Enter:
                    DisplayMaze();
                    break;

                case ConsoleKey.Escape:
                    QuitGame();
                    break;
            }
        }
        
            public void KeyMovement()
        {
            ConsoleKey cki;
            cki = Console.ReadKey(true).Key;
            switch (cki)
         
            {
                case ConsoleKey.UpArrow:
                    {
                        int[] location = PlayerLocation();
                        string[] maze = GetMaze();

                        if (maze[playery + 1].Equals(" "))
                        {
                            location[1] = location[1] + 1;
                            SetPlayerY(GetPlayerY() + 1);
                            step++;
                        }
                        else
                            Console.WriteLine("Invalid Move");
                            break;
                    }
                case ConsoleKey.DownArrow:
                    {
                        int[] location = PlayerLocation();
                        string[] maze = GetMaze();

                        if (maze[playery - 1].Equals(" "))
                        {
                            location[1] = location[1] - 1;
                            SetPlayerY(GetPlayerY() - 1);
                            step++;
                        }
                        else
                            Console.WriteLine("Invalid Move");
                        break;
                    }

                case ConsoleKey.LeftArrow:
                    {
                        int[] location = PlayerLocation();
                        string[] maze = GetMaze();

                        if (maze[playerx - 1].Equals(" "))
                        {
                            location[0] = location[0] - 1;
                            SetPlayerX(GetPlayerX() - 1);
                            step++;
                        }
                        else
                            Console.WriteLine("Invalid Move");
                        break;
                    }
                case ConsoleKey.RightArrow:
                    {
                        int[] location = PlayerLocation();
                        string[] maze = GetMaze();

                        if (maze[playerx + 1].Equals(" "))
                        {
                            location[0] = location[0] + 1;
                            SetPlayerX(GetPlayerX() + 1);
                            step++;
                        }
                        else
                            Console.WriteLine("Invalid Move");
                        break;
                    }
            }   
        }


    }
     
    
    public static void Main(string[] args)
    {
        Maze game = new Maze();
        game.TitleScreen();
       

    }
 
   
    
    
}

}

  • The part that VS is pointing to is specifically string[]map = GetMaze(); – alairroberts May 06 '21 at 00:52
  • We would need to see what GetMaze() looks like in order to help – person27 May 06 '21 at 00:55
  • public string[] GetMaze() { return mazeMap; } my maze is too big to post on here since its an array, i know its not efficient but thats what is required of me – alairroberts May 06 '21 at 00:59
  • 1
    I question if the line of code… `string[]map = GetMaze();` … would throw a `null` exception. Even if `GetMaze()` returned `null`, it will not throw an exception. Are you sure that line of code throws the exception? – JohnG May 06 '21 at 01:04
  • apologies, its my for loop under it for (int x = 0; x < map.Length; x++) that is what is returning the null – alairroberts May 06 '21 at 01:06
  • Are you sure, `GetMaze` is actually returning a value? `map` is the ONLY thing in the `for` construct that could be `null` – JohnG May 06 '21 at 01:11
  • I added the rest of my code if that helps. It was returning my maze before i added the if statement in my DisplayMaze method. I commented the if statement out and it was still returning null but when i tested it, it was working properly – alairroberts May 06 '21 at 01:14
  • It appears obvious that somewhere/somehow `GetMaze` is returning a `null` value. I suggest you trace the code. – JohnG May 06 '21 at 01:18
  • In the `TitleScreen` method where is the variable `title` defined? – JohnG May 06 '21 at 01:21
  • my title is ASCII art, too big for the website, but it is put into an array just like my maze. the `TitleScreen` method works more or less until it calls on `DisplayMaze`. – alairroberts May 06 '21 at 01:23
  • Even if what you say is true… we, who are trying to help you, must assume that `title` is `null` this is what the code shows. So who are we to believe… your word or the compilers error using the posted code? – JohnG May 06 '21 at 01:30
  • Helping you with this little information is probably a dead end. I don't quite know what you problem you are having with the code, but this is someone who might have had a similar problem. https://stackoverflow.com/questions/15102929/null-reference-exception-when-calling-an-object-array – DavvveN May 06 '21 at 01:06

0 Answers0