2011-01-11 46 views
2

I次のコードを持っている -オブジェクト参照がオブジェクトインスタンスに設定されていません

private static void convert() 
    { 
     string csv = File.ReadAllText("test.csv"); 
     XDocument doc = ConvertCsvToXML(csv, new[] { "," }); 
     doc.Save("update.xml"); 

     XmlTextReader reader = new XmlTextReader("update.xml"); 
     XmlDocument testDoc = new XmlDocument(); 
     testDoc.Load(@"update.xml"); 

     XDocument turnip = XDocument.Load("update.xml"); 
     webservice.function[] test = new webservice.function[1]; 
     webservice.function CallWebService = new webservice.function(); 

     foreach(XElement el in turnip.Descendants("row")) 
     { 
          test[0].com = System.Convert.ToInt32(el.Descendants("var").Where(x => (string)x.Attribute("name") == "com").SingleOrDefault().Attribute("value").Value); 
      test[0].Centre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "Centre").SingleOrDefault().Attribute("value").Value; 
      test[0].CCentre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "CCentre").SingleOrDefault().Attribute("value").Value; 

      MessageBox.Show(test[0].person, "person"); 
      MessageBox.Show(System.Convert.ToString(test[0].actually), "Actually"); 
      MessageBox.Show(System.Convert.ToString(test[0].com), "Com"); 

      CallWebService.updateFeedStatus(test); 
     } 

それはのエラーを考え出すされた - とNullReferenceExceptionがオブジェクト参照がインスタンスに設定されていないことを言って、未処理でしたオブジェクト。このエラーは、最初の行test [0] .accountで発生します。

どうすればこのことができますか?

+2

投稿するstacktraceしてください、そうでなければどこにでもある可能性があります。 – BrokenGlass

+1

暗闇の中でデバッグしています - どのような行ですか? –

+0

申し訳ありません私は編集しました。 – Ebikeneser

答えて

6

アレイを初期化しても、アレイ内のオブジェクトは初期化されません。下の2番目の行を追加してみてください(デフォルトのコンストラクタを使用したいと仮定します):私はあなたのxmlを推測している

webservice.singleSummary[] test = new webservice.singleSummary[1]; 
test[0] = new webservice.singleSummary(); 
2
  1. プロセスにデバッガを配置します。
  2. エラーを生成するコード行を特定します。 (Visual Studioを想定)
  3. null参照があるものを特定するまで、その行のオブジェクト参照を1つずつテストします。
  4. 問題を解決するために行の前にヌルチェックを入れてください。
1

は、あなたが希望の要素のためのあなたのXNameに含める必要がある名前空間を持っていますが、他のようにコメントに言及すると、完全な開示なしに行うことができるものはほとんどありません。

関連する問題