C
C#3mo ago
MONO

Cant get my list to work

Im trying to make a WPF application for movies, where i can look up a move, and get some info. But i cant get the JSON content into the page. I constanly get different errors, and i cant figger this one out anymore. Even AI's are give me all kinda solutions, but end up going in circle. This is my MovieViewModel.cs:
c#
private async void InitializeViewModel()
{
var MovieURL = "https://xxxxx/page-1.json";
var movieCollectionList = new List<MovieInfo>();

using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(MovieURL);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var APIsearchResault = JsonConvert.DeserializeObject<dynamic>(content);

try
{
if (APIsearchResault.Response!= null)
{
foreach (var movie in APIsearchResault.Search)
{
Console.WriteLine(movie.Title);
movieCollectionList.Add(
new MovieInfo
{
Title = movie.Title,
TmdbId = movie.TmdbId,
}
);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
}
}

Movies = movieCollectionList;

_isInitialized = true;
}
c#
private async void InitializeViewModel()
{
var MovieURL = "https://xxxxx/page-1.json";
var movieCollectionList = new List<MovieInfo>();

using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(MovieURL);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var APIsearchResault = JsonConvert.DeserializeObject<dynamic>(content);

try
{
if (APIsearchResault.Response!= null)
{
foreach (var movie in APIsearchResault.Search)
{
Console.WriteLine(movie.Title);
movieCollectionList.Add(
new MovieInfo
{
Title = movie.Title,
TmdbId = movie.TmdbId,
}
);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
}
}

Movies = movieCollectionList;

_isInitialized = true;
}
This is the JSON content:
{
"result": [
{
"imdb_id": "tt9281138",
"tmdb_id": "972423",
"title": "A Million Days 2023",
}, .....
{
"result": [
{
"imdb_id": "tt9281138",
"tmdb_id": "972423",
"title": "A Million Days 2023",
}, .....
But i keep getting error: System.NullReferenceException: 'Object reference not set to an instance of an object.' Im lost, need some help, plz
No description
No description
4 Replies
Saber
Saber3mo ago
what makes you think APIsearchResault.Search is valid based on that json result
MONO
MONO3mo ago
thougth it was valid since it had data, buuut, i can almost guess im wrong here Do you know how i can make the JSON work into my project, if its not valid?
Saber
Saber3mo ago
where do you see a property called Search in that json object?
MONO
MONO3mo ago
Oh sorry, mixed up the code. The search is in a list i made. <MovieSearchResaults>
c#
var APIsearchResault = JsonConvert.DeserializeObject<MovieSearchResult>(content);
c#
var APIsearchResault = JsonConvert.DeserializeObject<MovieSearchResult>(content);
That goes into my MovieModel:
c#
public class MovieSearchResult
{
public List<MovieInfo> Search { get; set; }
public string Response { get; set; }
public string Error { get; set; }
}
public struct MovieInfo
{
public string TmdbId { get; set; }
public string Title { get; set; }
}
c#
public class MovieSearchResult
{
public List<MovieInfo> Search { get; set; }
public string Response { get; set; }
public string Error { get; set; }
}
public struct MovieInfo
{
public string TmdbId { get; set; }
public string Title { get; set; }
}
Thats where i get the search.