Here is a basic math programm in which all I simply want to do is save these values num1
and num2
into the list saved within the class. This keeps throwing this error when the program gets to the line in which it is added.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I know that what I have done wrong is something obvious but I am not sure at the moment.
class Program
{
static void Main(string[] args)
{
PromptAndAddUserNums();
}
public static void PromptAndAddUserNums() {
bool goToken = true;
UserInfo userInfo = new UserInfo();
while (goToken)
{
Console.WriteLine("insert 1st number");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("insert 2nd number");
int num2 = int.Parse(Console.ReadLine());
userInfo.NumList.Add(num1);
userInfo.NumList.Add(num2);
Console.WriteLine("do you wanna add another number?(yes or no)");
string userChoice = Console.ReadLine().ToUpper();
if (userChoice == "YES")
{
continue;
}
else if (userChoice == "NO")
{
goToken = false;
}
}
}
}
class UserInfo{
public List<int> NumList { get; set; }
public UserInfo()
{
}
}