Answer the question
In order to leave comments, you need to log in
Why does System.NullReferenceException: 'Object reference not set to an instance of an object occur?
Hello. There is this method:
[HttpPost]
[Route("postTourInfo")]
public string Post([FromBody] GettingTourInfo value)
{
Tours tours = new Tours();
tours.tourInfos[0].beachType = "one";// здесь возникает ошибка
tours.tourInfos[1].beachType = "two";
return JsonConvert.SerializeObject(tours);
}
public class GettingTourInfo
{
public string fromCity { get; set; }
public string toCountry { get; set; }
public DateTime departureDate { get; set; }
public int amountNights { get; set; }
public int amountAdults { get; set; }
}
public class TourInfo
{
public int statusCode { get; set; } //200, если туры найдены, 404 - если не найдены
public string tourName { get; set; } //названиеТура
public string hotelName { get; set; } //названиеОтеля
public int hotelRating { get; set; } //рейтингОтеля
public int line { get; set; } //линия
public string beachType { get; set; } //типа поляжа(песок, песок с галькой, галька, платформа)
public int distanceToBeach { get; set; }// растояние до пляжа
public int distanceToAirport { get; set; } // растояние до аэропорта
}
public class Tours
{
public List<TourInfo> tourInfos { get; set; }
}
System.NullReferenceException: 'Object reference not set to an instance of an object
Answer the question
In order to leave comments, you need to log in
Because tours.tourInfos
no one has made a list. You only declared it, but did not become new .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question