I want to build a console application where I am storing a list of cars linked to a person's id . My class is -
I am getting a null pointer exception( object reference object reference not set to an instance of an object) when I am trying to add cars to the list. Please see the code below-
I am also getting a warning for public List<string> carlist that it is not assigned and the default value will always be null.
How do I fix this ?
C#:
class Person
{
public int PersonID
{
get;
set;
}
public string cars
{
get;
set;
}
public List<string> carlist
{
get;
set;
}
}
C#:
do
{
Person person = new Person();
PersonDetails personDetails = new PersonDetails();
Console.WriteLine("Enter the Person ID");
person.PersonID = Convert.ToInt32(Console.ReadLine());
char response;
do
{
Console.WriteLine("Enter his cars");
person.cars = Console.ReadLine();
person.carlist.Add(person.cars);
Console.WriteLine("More cars ? Press y to contine");
response = Convert.ToChar(Console.ReadLine());
} while (response == 'y');
personList.Add(person);
Console.WriteLine("Press y if you wannt add data");
ch = Convert.ToChar(Console.ReadLine());
} while (ch == 'y');
I am also getting a warning for public List<string> carlist that it is not assigned and the default value will always be null.
How do I fix this ?
Last edited by a moderator: