I have come across an error code, after building during debug, it shows:

System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
This problem is located at the end of the program, on the else if statement.
Any suggestions as to how I could easily fix this problem, I need to get this project finished.
It has been a long couple of days fighting with it, even though I thought I wouldn't have that much trouble with such a simple program, I just can't seem to grasp the concept of this one.
Thanks.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Hangman_Game
    {
        /// Class Word
        public class Word
        {

            /// Class word
            public Word()
            {
            }

            /// Class word
            public Word(string content)
            {
                this.Content = content;
            }

            /// Get or set word content
            public string Content { get; set; }

            /// Get or set word length
            public int WordLength
            {
                get { return this.Content.Length; }
            }
        }
    }
    namespace Hangman_Game
    {
        /// Class which holds a collection of words for the hangman game
        public class Words : List<Word>
        {
            public Words()
            {
                this.Add(new Word() { Content = "VACATION" });
                this.Add(new Word() { Content = "INFORMATION" });
                this.Add(new Word() { Content = "TECHNOLOGY" });
                this.Add(new Word() { Content = "COMPUTER" });
                this.Add(new Word() { Content = "ROUTER" });
                this.Add(new Word() { Content = "PRINTER" });
                this.Add(new Word() { Content = "SOFTWARE" });
                this.Add(new Word() { Content = "HARDWARE" });
                this.Add(new Word() { Content = "EMPLOYEE" });
            }
            /// Pick a random word
            public Word Pick
            {
                get
                {
                    Random RandomPick = new Random();
                    int index = (int)(RandomPick.NextDouble() * this.Count);
                    Word word = this[index];
                    word.Content = word.Content.ToUpper();
                    return word;
                }
            }
        }
    }
    namespace Hangman_Game
    {
        /// Game result enumeration
        public enum GAMERESULT
        {
            WIN,// Game is finished and player won the game
            LOSE,// Game is finished and player lose the game
            CONTINUE,// Continue play
        }
    }

            namespace Hangman_Game
            {
                /// Main Program class
                class playHangman
                {
                    static void Main(string[] args)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Welcome to hangman. You get seven chances to guess the mystery word.");
                        string yesNo = string.Empty;
                        do
                        {
                            Console.WriteLine();
                            yesNo = playGame();
                        } while (yesNo.ToUpper().Equals("Y"));
                    }

                    /// Play game
                    public static string playGame()
                    {
                        Words words = new Words();
                        Word pickedWord = words.Pick;
                        PlayHangman playHangman = new PlayHangman();
                        Word PickedWord = pickedWord;
                        ConsoleKeyInfo yesNo = new ConsoleKeyInfo();

                        for (int i = 0; i < pickedWord.WordLength; i++)
                        {
                            Console.Write(" * ");
                        }
                        Console.WriteLine();
                        Console.WriteLine();
                        Console.WriteLine();
                        while (playHangman.Result() == Hangman_Game.GAMERESULT.CONTINUE)
                        {
                            Console.Write("Pick a letter ---> ");
                            ConsoleKeyInfo GuessedLetters = Console.ReadKey();


                        }
                        if (playHangman.Result() == Hangman_Game.GAMERESULT.LOSE)
                        {
                            Console.WriteLine("Sorry, you didn't guess correctly. ");
                            Console.WriteLine("The mystery word was '" + pickedWord.Content.ToUpper() + "'", 500);
                            Console.WriteLine("Do you want to play again ? Y/N");
                            yesNo = Console.ReadKey();
                            return yesNo.KeyChar.ToString();
                        }
                        else
                        {
                            Console.WriteLine("You won!", 500);
                            Console.WriteLine("Do you want to play again ? Y/N");
                            yesNo = Console.ReadKey();
                            return yesNo.KeyChar.ToString();
                        }
                    }
                }
            }
            namespace Hangman_Game
            {
                /// Class play hangman game
                public class PlayHangman
                {

                    /// Get or set the random picked word
                    public Word PickedWord
                    {
                        get;
                        set;
                    }

                    /// Variable used to hold guessed and found letters
                    public List<string> guessed_FoundLetters;

                    /// Variable used to hold user guessed letters
                    public List<string> guessedLetters;

                    /// Variable used to hold user missed words from the word
                    public List<string> MissedLetters;

                    /// Class play hang man game
                    public PlayHangman()
                    {
                        guessed_FoundLetters = new List<string>();
                        guessedLetters = new List<string>();
                        MissedLetters = new List<string>();
                    }

                    /// Process the user guessed letter against the random picked word
                    public void Play()
                    {
                        for (int i = 0; i < PickedWord.WordLength; i++) // Add asterick to the guessed and found string collection
                        {
                            guessed_FoundLetters.Add(" * ");
                        }

                        for (int i = 0; i < PickedWord.WordLength; i++)
                        {
                            string letter = PickedWord.Content.Substring(i, 1);
                            if (guessedLetters.Count > 0)
                            {
                                foreach (string guessedLetter in this.guessedLetters)
                                {
                                    if (letter.Equals(guessedLetter.Trim().ToUpper())) // If the guessed letter is found from the picked word then replace asterick with the letter
                                    {
                                        guessed_FoundLetters.RemoveAt(i);
                                        guessed_FoundLetters.Insert(i, " " + letter + " ");
                                    }
                                }
                            }
                        }

                        Console.WriteLine(buildString(guessed_FoundLetters, false));
                        Console.WriteLine();
                    }

                    /// Add user guessed letter
                    public bool AddGuessedLetters(char letter)
                    {
                        if (char.IsDigit(letter))  // Check if the letter is a digit
                        {
                            Console.WriteLine();
                            Console.WriteLine("'" + letter.ToString().ToUpper() + "' is not a valid letter");
                            return false;
                        }
                        else if (!this.guessedLetters.Contains(letter.ToString().ToUpper())) // Add guessed letter if it does not exist
                        {
                            this.guessedLetters.Add(letter.ToString().ToUpper());
                            Console.WriteLine();
                            Console.WriteLine("Guessed Letters : " + buildString(guessedLetters, true));
                            return true;
                        }
                        else // letter exists
                        {
                            Console.WriteLine();
                            Console.WriteLine("Sorry, you already guessed '" + letter.ToString().ToUpper() + "'");
                        }
                        return false;
                    }

                    /// Check a letter is found from the random picked word
                    public bool checkLetter(string letter)
                    {
                        for (int i = 0; i < PickedWord.WordLength; i++)
                        {
                            string splitedletter = PickedWord.Content.Substring(i, 1).ToUpper();
                            if (splitedletter.Equals(letter.Trim().ToUpper()))
                            {
                                return true;
                            }
                        }
                        return false;
                    }

                    /// Build a string from string collection
                    public string buildString(List<string> inPutString, bool space)
                    {
                        StringBuilder outPut = new StringBuilder();
                        foreach (object item in inPutString)
                        {
                            if (space)
                                outPut = outPut.Append(item.ToString().ToUpper() + " ");
                            else
                                outPut = outPut.Append(item.ToString().ToUpper());

                        }
                        return outPut.ToString();
                    }

                    /// Process win lose the game
                    public GAMERESULT Result()
                    {
                        if (MissedLetters.Count == 7)//If full hang man is built then, you lose the game
                        {
                            return GAMERESULT.LOSE;
                        }
                        else if (PickedWord.Content.ToUpper().Equals(buildString(guessed_FoundLetters, false).Replace(" ", " "))) // If the guessed letters match all the letters from random picked word, then you win the game
                        {
                            return GAMERESULT.WIN;
                        }
                        else
                            return GAMERESULT.CONTINUE; // Else play the game
                    }
                }
            }

The problem arises because the PickedWord is null in this context.

Be aware that C# is capital aware so the playHangman class for the main program and the PlayHangman class gor the game management are not the same.

So when in the playhangman class you wrote

  1. PlayHangman playHangman = new PlayHangman();
  2. Word PickedWord = pickedWord;

probably you'll need

97.                        PlayHangman playHangman = new PlayHangman();
98.                        playHangman.PickedWord = pickedWord;

I also will suggest not to reuse the class name as a variable name because may be confusing.

Hope this helps.

Thanks! That fixed my "null" problem, now I have an debugging issue I'm going to try and fix. I might be back in a couple hours. . .

I've come to a hault in my readings and debugging and can't seem to figure out where the code is going wrong. After I start the debugging, it says the Welcome to hangman! You only have seven guess, gives the * * * * * * * , and asks for you to pick a letter. I pick a letter and it gets stuck within the loop. I hate being stuck in a infinite loop. Can anyone give me any pointers of getting out? I've already tried placing a break; point and it goes straight to YOU WIN! When I had only put in 1 letter.

This is the revised version, that currently sends me into an infinite loop asking to pick a letter. I know all I need to do is either add something, or move a couple sections around, maybe? I'm completely lost on the matter and I have been trying almost non stop. I feel terrible that it is due tomorrow and I can't even get out of a simple loop. Does anyone know of any easy way to make a quick fix?

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Hangman_Game
    {
        /// Class Word
        public class Word
        {
            /// Class word
            public Word()
            {
            }
            /// Class word
            public Word(string content)
            {
                this.Content = content;
            }
            /// Get or set word content
            public string Content { get; set; }
            /// Get or set word length
            public int WordLength
            {
                get { return this.Content.Length; }
            }
        }
    }
    namespace Hangman_Game
    {
        /// Class which holds a collection of words for the hangman game
        public class Words : List<Word>
        {
            public Words()
            {
                this.Add(new Word() { Content = "VACATION" });
                this.Add(new Word() { Content = "INFORMATION" });
                this.Add(new Word() { Content = "TECHNOLOGY" });
                this.Add(new Word() { Content = "COMPUTER" });
                this.Add(new Word() { Content = "ROUTER" });
                this.Add(new Word() { Content = "PRINTER" });
                this.Add(new Word() { Content = "SOFTWARE" });
                this.Add(new Word() { Content = "HARDWARE" });
                this.Add(new Word() { Content = "EMPLOYEE" });
            }
            /// Pick a random word
            public Word Pick
            {
                get
                {
                    Random RandomPick = new Random();
                    int index = (int)(RandomPick.NextDouble() * this.Count);
                    Word word = this[index];
                    word.Content = word.Content.ToUpper();
                    return word;
                }
            }
        }
    }
    namespace Hangman_Game
    {
        /// Game result enumeration
        public enum GAMERESULT
        {
            WIN,// Game is finished and player won the game
            LOSE,// Game is finished and player lose the game
            CONTINUE,// Continue play
        }
    }
            namespace Hangman_Game
            {
                /// Main Program class
                class playHangman
                {
                    static void Main(string[] args)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Welcome to hangman. You get seven chances to guess the mystery word.");
                        string yesNo = string.Empty;
                        do
                        {
                            Console.WriteLine();
                            yesNo = playGame();
                        } while (yesNo.ToUpper().Equals("Y"));
                    }
                    /// Play game
                    public static string playGame()
                    {
                        Words words = new Words();
                        Word pickedWord = words.Pick;
                        PlayHangman playHangman = new PlayHangman();
                        playHangman.PickedWord = pickedWord;
                        Word PickedWord = pickedWord;
                        ConsoleKeyInfo yesNo = new ConsoleKeyInfo();

                        for (int i = 0; i < pickedWord.WordLength; i++)
                        {
                            Console.Write(" * ");
                        }
                        Console.WriteLine();
                        Console.WriteLine();
                        Console.WriteLine();
                        while (playHangman.Result() == Hangman_Game.GAMERESULT.CONTINUE)
                        {
                            Console.Write("Pick a letter ---> ");
                            ConsoleKeyInfo guessedLetters = Console.ReadKey();
                            break;
                        }
                        if (playHangman.Result() == Hangman_Game.GAMERESULT.LOSE)
                        {
                            Console.WriteLine("Sorry, you didn't guess correctly. ");
                            Console.WriteLine("The mystery word was '" + pickedWord.Content.ToUpper() + "'", 500);
                            Console.WriteLine("Do you want to play again ? Y/N");
                            yesNo = Console.ReadKey();
                            return yesNo.KeyChar.ToString();

                        }
                        else
                        {
                            Console.WriteLine("You won!", 500);
                            Console.WriteLine("Do you want to play again ? Y/N");
                            yesNo = Console.ReadKey();
                            return yesNo.KeyChar.ToString();
                        }
                    }
                }
            }
            namespace Hangman_Game
            {
                /// Class play hangman game
                public class PlayHangman
                {
                    /// Get or set the random picked word
                    public Word PickedWord
                    {
                        get;
                        set;
                    }
                    /// Variable used to hold guessed and found letters
                    public List<string> guessed_FoundLetters;
                    /// Variable used to hold user guessed letters
                    public List<string> guessedLetters;
                    /// Variable used to hold user missed words from the word
                    public List<string> MissedLetters;
                    /// Class play hangman game
                    public PlayHangman()
                    {
                        guessed_FoundLetters = new List<string>();
                        guessedLetters = new List<string>();
                        MissedLetters = new List<string>();
                    }
                    /// Process the user guessed letter against the random picked word
                    public void Play()
                    {
                        for (int i = 0; i < PickedWord.WordLength; i++) // Add asterick to the guessed and found string collection
                        {
                            guessed_FoundLetters.Add(" * ");
                        }
                        for (int i = 0; i < PickedWord.WordLength; i++)
                        {
                            string letter = PickedWord.Content.Substring(i, 1);
                            if (guessedLetters.Count > 0)
                            {
                                foreach (string guessedLetter in this.guessedLetters)
                                {
                                    if (letter.Equals(guessedLetter.Trim().ToUpper())) // If the guessed letter is found from the picked word then replace asterick with the letter
                                    {
                                        guessed_FoundLetters.RemoveAt(i);
                                        guessed_FoundLetters.Insert(i, "'" + letter + "'");
                                    }
                                }
                            }
                        }
                        Console.WriteLine(buildString(guessed_FoundLetters, false));
                        Console.WriteLine();
                    }
                    /// Add user guessed letter
                    public bool AddGuessedLetters(char letter)
                    {
                        if (char.IsDigit(letter))  // Check if the letter is a digit
                        {
                            Console.WriteLine();
                            Console.WriteLine("'" + letter.ToString().ToUpper() + "' is not a valid letter");
                            return false;
                        }
                        else if (!this.guessedLetters.Contains(letter.ToString().ToUpper())) // Add guessed letter if it does not exist
                        {
                            this.guessedLetters.Add(letter.ToString().ToUpper());
                            Console.WriteLine();
                            Console.WriteLine("Guessed Letters: " + buildString(guessedLetters, true));
                            return true;
                        }
                        else // letter exists
                        {
                            Console.WriteLine();
                            Console.WriteLine("Sorry, you already guessed '" + letter.ToString().ToUpper() + "'");
                        }
                        return false;
                    }
                    /// Check if letter is found from the random picked word
                    public bool checkLetter(string letter)
                    {
                        for (int i = 0; i < PickedWord.WordLength; i++)
                        {
                            string splitedletter = PickedWord.Content.Substring(i, 1).ToUpper();
                            if (splitedletter.Equals(letter.Trim().ToUpper()))
                            {
                                return true;
                            }
                        }
                        return false;
                    }
                    /// Build a string from string collection
                    public string buildString(List<string> inPutString, bool space)
                    {
                        StringBuilder outPut = new StringBuilder();
                        foreach (object item in inPutString)
                        {
                            if (space)
                                outPut = outPut.Append(item.ToString().ToUpper() + " ");
                            else
                                outPut = outPut.Append(item.ToString().ToUpper());
                        }
                        return outPut.ToString();
                    }
                    /// Process win or lose the game
                    public GAMERESULT Result()
                    {
                        if (MissedLetters.Count == 7)//If full hang man is built then, you lose the game
                        {
                            return GAMERESULT.LOSE;
                        }
                        else if (PickedWord.Equals(buildString(guessed_FoundLetters, false))) // If the guessed letters match all the letters from random picked word, then you win the game
                        {
                            return GAMERESULT.WIN;
                        }
                        else
                            return GAMERESULT.CONTINUE; // Else play the game
                    }
                }
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.