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