問題は他のケースである私は、この他の場合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();
}
}
- アイテムがnull&あなたはWriteLineメソッド文でのvalフィールドを表示しようとしている - そうitem.valは、例外が発生。 – PaulF
tKnidはどこに定義されていますか? Paulは、tKnidがconsole.Readlineから入力を受け取った後にエラーがitem変数をnullとして指していることを正しく示しています。そこにブレークポイントを置き、値が何であるかを見てください。 – JohnG
私は、上記のコードがコンパイル時に ''現在のコンテキストに 'tknid 'という名前のtknidが存在しないと確信しています。' tknid = Console.ReadLine(); ' – JohnG