リスト内のリストからjsonデータを返そうとしています。データはWebクライアントを使用して取得され、JSON.NETを使用して逆シリアル化されます。私は "results"リストの中にある "featuredCharts"リストから名前とイメージを返したいと思います。ここで私が参照しているjsonデータの一部です。ネストされたforeachループのNullReferenceException WP7
"results":{
"featuredCharts":[
{
"id":46082,
"type":"chart",
"name":"Exclusives On Beatport - Week 5",
"slug":"exclusives-on-beatport-week-5",
"description":"",
"publishDate":"2012-01-30",
"price":{
"code":"usd",
"symbol":"$",
"value":2390
},
"audioFormatFee":{
"wav":{
"code":"usd",
"symbol":"$",
"value":1000
},
"aiff":{
"code":"usd",
"symbol":"$",
"value":1000
}
},
"genres":[
{
"id":11,
"name":"Tech House",
"slug":"tech-house",
"type":"genre"
},
{
"id":5,
"name":"House",
"slug":"house",
"type":"genre"
},
{
"id":17,
"name":"Electro House",
"slug":"electro-house",
"type":"genre"
},
{
"id":15,
"name":"Progressive House",
"slug":"progressive-house",
"type":"genre"
}
],
"images":{
"small":{
"width":30,
"height":30,
"url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951247.jpg",
"secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951247.jpg"
},
"medium":{
"width":60,
"height":60,
"url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951248.jpg",
"secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951248.jpg"
},
"large":{
"width":130,
"height":130,
"url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951249.jpg",
"secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/40\/4951249.jpg"
},
"xlarge":{
"width":500,
"height":500,
"url":"http:\/\/geo-media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/50\/4951250.jpg",
"secureUrl":"https:\/\/media.beatport.com\/items\/imageCatalog\/4000000\/900000\/50000\/1000\/200\/50\/4951250.jpg"
}
},
"chartOwner":null
},
現在のところ、クラスはこのように設定されています。
public class NewReleasesCharts //Root Object
{
public Metadata metadata { get; set; }
public List<ResultHome> results = new List<ResultHome>();
public IEnumerator<ResultHome> GetEnumerator()
{
return this.results.GetEnumerator();
}
}
public class ResultHome
{
public List<FeaturedCharts> featuredCharts { get; set; }
public List<FeaturedReleases> featuredReleases { get; set; }
}
public class FeaturedCharts
{
public int id { get; set; }
public string type { get; set; }
public string name { get; set; }
public string slug { get; set; }
public ChartImages chartImages { get; set; }
}
public class ChartImages
{
public ChartSmall chartSmall { get; set; }
public ChartMedium chartMedium { get; set; }
public ChartLarge chartLarge { get; set; }
}
public class ChartMedium
{
public int width { get; set; }
public int height { get; set; }
public string url { get; set; }
public string secureUrl { get; set; }
}
これは私が立ち往生している部分です。データをデシリアライズした後は、ネストされたforeachループを使用していますが、現在内部foreachループのrc.featuredCharts
にNullReferenceExceptionエラーが発生しています。ここにコードがあります。
// Deserialize home page data
void jsonHome_GetDataCompleted(object snder, DownloadStringCompletedEventArgs e)
{
NewReleasesCharts homeData = JsonConvert.DeserializeObject<NewReleasesCharts>(e.Result);
try
{
// Nested foreach loops to dispaly data
foreach (ResultHome rc in homeData)
{
foreach (FeaturedCharts fc in rc.featuredCharts)
{
// TODO: return name and image of chart
string name = fc.name;
listGenres.Items.Add(name);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
私はまだ間違っているのか分からないので、私はまだ初心者です。これを適切に行う方法の例は、私のアプリの他の部分でこれを行う必要があるので、私は何かを構築するために何かを与えるだろう。
ありがとうございました。ここ
UPDATEフル例外である:
System.NullReferenceException was unhandled
メッセージ=とNullReferenceException のStackTrace:Beatport.MainPage.jsonHome_GetDataCompleted(オブジェクトsnder、DownloadStringCompletedEventArgs e)に System.Net.WebClient.OnDownloadStringCompletedに( DownloadStringCompletedEventArgs e) at System.Net.WebClient.DownloadStringOperationCompleted(Object arg) at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi、Object obj、BindingFlags invokeAttrバインドバインダー、オブジェクト[]パラメーター、CultureInfoカルチャ、バインダーバインダー、オブジェクトバインダー、バインダーバインダー、CultureInfoカルチャ、ブールバインダー、BooleanInBindingDefault、 StackCrawlMark & stackMark)System.Reflection.MethodBase.Invokeで System.Delegate.DynamicInvokeOneで(オブジェクトobj、オブジェクト[]パラメータ) ([]引数オブジェクト)System.MulticastDelegate.DynamicInvokeImpl(オブジェクト[]引数で ) でSystem.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)でSystem.Windows.Threading.DispatcherOperation.Invoke() のSystem.Delegate.DynamicInvoke(Object [] args) System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object [] args)の で 、System.Windows.Hosting.CallbackCookie.Invoke(Object [] args)のSystem.Windows.Threading.Dispatcher.OnInvokeでを返します。 System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(のIntPtr pHandle、のInt32 nParamCount、ScriptParam [] pParams、ScriptParam & PRESULT)
[.NETのNullReferenceExceptionは何ですか?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) –
また、私はあなたから抜け出すことをお勧めしますどこでもtry/catchブロックを使用する習慣。一般的には、例外を実用的な高さでスタック内に伝播させることをお勧めします。だからあなたのコードに外部の 'try/catch/MessageBox.Show(ex.ToString());;)があるはずです。ex.Messageはメッセージテキストのみを出力するので、完全な例外を表示したいことに注意してください。 –
コードを見るのは難しいですが、 'foreach(FeaturedCharts fc in rc.featuredCharts) 'にブレークポイントを設定した場合、' rc.featuredCharts'の内容は何ですか? – Alan