2016-11-04 7 views
-1

問題は他のケースである私は、この他の場合nullreferenceexceptionを引き起こす文字列の入力に関するエラーは何ですか?

static void Main(string[] args) 
    { 
     XMLData xmldataExample = new XMLData(); 
     Usergaz usergazExample = new Usergaz(); 
     UsergazItem usergazitemExample = new UsergazItem(); 

     UsergazItem item = new UsergazItem(); 
     string filename = @"C:\Users\565133\Desktop\test.xml"; 
     Deserialize<XMLData> deserializeobj = new Deserialize<XMLData>(); 
     Console.WriteLine("Choose \n1.Serialization\n2.Deserialization\n3.Deserialize only a token"); 

     //It works when I give tknid input line here 
     int inp = Console.Read(); 
     string tknid = Console.ReadLine();//It doesn't work if I get tknid input here 

     if (inp == 1) 
     { 
      usergazitemExample.getusergazitem(); 

      usergazExample.getusergaz(); 
      usergazExample.gazitem.Add(usergazitemExample); 

      xmldataExample.getxmldata(); 
      xmldataExample.gazset.Add(usergazExample); 


      serial(xmldataExample, filename); 
      Console.WriteLine("Its done"); 
      Console.ReadKey(); 

     } 
     else if (inp == 2) 
     { 
      Console.WriteLine("Getting data from xml file"); 

      // Deserialize<XMLData> deserializeobj = new Deserialize<XMLData>(); 
      xmldataExample = deserializeobj.deserializefunction(filename); 
      List<Usergaz> samplelist = new List<Usergaz>(); 
      samplelist = xmldataExample.gazset; 

      MTable.initialize(); 
      MTable.usergazzetterset.Add(xmldataExample.updatedtime, samplelist); 
      Console.WriteLine("Deserialization complete, check the object"); 
      Console.ReadKey(); 
     } 

問題の行にコメントしている、私はtknidを使用してフィルタリングんだけど、でも、ユーザーからのI入力tknid前に、私はnullreferenceの例外を取得します'item'オブジェクトがnullであると言っています。 私は、例外が、私は解決策を持っているConsole.WriteLineをライン

 else 
     { 
      //Console.WriteLine("Getting only a specific token Enter the token id"); 
      //string tknid; 
      //tknid = Console.ReadLine(); 
//I intended to give tknid input here, but I cannot do it... 
      //Console.WriteLine(tknid); 
      //Console.ReadKey(); 
      //string tknid = "3"; Initializing tknid with this statement works fine 




      item = deserializeobj.deserializefunction(filename).gazset[0].gazitem.Where(X => X.id == tknid).FirstOrDefault(); 

      Console.WriteLine("The value for the token id {0} is {1}", tknid,item.val); 

      Console.ReadKey(); 

     } 
    } 
+1

- アイテムがnull&あなたはWriteLineメソッド文でのvalフィールドを表示しようとしている - そうitem.valは、例外が発生。 – PaulF

+0

tKnidはどこに定義されていますか? Paulは、tKnidがconsole.Readlineから入力を受け取った後にエラーがitem変数をnullとして指していることを正しく示しています。そこにブレークポイントを置き、値が何であるかを見てください。 – JohnG

+0

私は、上記のコードがコンパイル時に ''現在のコンテキストに 'tknid 'という名前のtknidが存在しないと確信しています。' tknid = Console.ReadLine(); ' – JohnG

答えて

0

を指摘し、問題がif..else文ではなかった、それは)Console.Read(でのget。

int inp = Console.Read()int inp = int.Parse(Console.ReadLine())に置き換えても問題ありません。

今私はConsole.Read()は、私は以下の共有した答えの助けを借りて動作しますが、私はConsole.Read()を使用しようとすることは決してないだろう、と常にConsole.ReadLine()を使用する方法を理解しています。

方法Console.Read()作品:あなたが理解していない例外のどの部分

https://stackoverflow.com/a/10318917/7114583

関連する問題