首页 > 解决方案 > Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“无法在空引用上执行运行时绑定”

问题描述

我从 OMDB API 获取电影 API,我想将 API 中的数据分配给字符串。

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“无法在空引用上执行运行时绑定” 但我收到此错误。有什么解决办法?

public MainViewModel()
{
    SearchCommand = new RelayCommand((e) =>
    {
        HttpResponseMessage response = new HttpResponseMessage();
        var name = SearchText;
        response = http.GetAsync($@"http://www.omdbapi.com/?apikey=a91a5037&t={name}&plot=full").Result;
        var str = response.Content.ReadAsStringAsync().Result;
        Data = JsonConvert.DeserializeObject(str);
        MoviePosterPath = Data.Search[0].Poster; // I'm getting errors here
        MovieTitle = Data.Search[0].Title;
        MovieIMDB = Data.Search[0].imdbRating;
        MovieYear = Data.Search[0].Released;
    });
} 

标签: c#wpfapi

解决方案


根据您想要获取详细信息的文档Search。在这种情况下,url 中的参数应该是snot t

response = http.GetAsync($@"http://www.omdbapi.com/?apikey=a91a5037&s={name}&plot=full").Result;

您的其余代码可以保持不变。


推荐阅读