-1

In the same class I have method CheckGuess, that take an integer from user input and check the price, if it not correct it should add it to previousGuesses and then display all the user guesses when he got the right answer.

In my program class after the actual game finish and user guessed the price I need to display statistics with his number of tries and all the guesses:

\

The problem is that I am just learning C# and I can't figure out how to correctly add Product price and description that user guessed to the property list

here is my error message I am getting: enter image description here any help appriciated

Also when is it the right way to print the property values that are in the saved list?

Console.WriteLine("Previous guesses:" + game.PreviousGuesses);
ANNA
  • 45
  • 5
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Aleks Andreev Jun 03 '18 at 17:49

2 Answers2

1

You haven't initialized _previousGuesses, so it's null.

private List<Product> _previousGuesses = new List<Product>();

should do.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Yes, I see! Thank you. But now it doesn't print price and description, it prints : System.Collections.Generic.List`1[firstName_lastName.Product] – ANNA Jun 03 '18 at 17:49
  • @ANNA Yes, that's what `.ToString()` for a list does :) You'll need to print the contents yourself. – AKX Jun 04 '18 at 09:57
1

In line:

 private List<Product> _previousGuesses;

_previousGuesses is not initialized use it like:

private List<Product> _previousGuesses = new List<Product>(); 
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171