-1

The problem is with the else case, I have commented on the problem lines

    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();
        }

In this else case, I'm filtering using tknid, but even before I input tknid from the user, I get nullreference exception saying the 'item' object is null. I get the exception pointed to the Console.WriteLine Line

        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
    What part of the exception are you not understanding - item is null & you are trying to display the val field in the writeline statement - so item.val causes the exception. – PaulF Nov 04 '16 at 11:21
  • Where is tKnid defined? Paul is correct that the error points to the item variable as being null because after tKnid receives input from the console.Readline its a string. Put a break point there and see what the values are. – JohnG Nov 04 '16 at 11:46
  • I am pretty sure the above code will throw a compile time error `The name 'tknid' does not exist in the current context` at line: `tknid = Console.ReadLine();` – JohnG Nov 04 '16 at 11:56
  • @PaulF 'item' object exists for tknid = 3, so, if I input 3 from the console, it should give me a item.val, setting string tknid = "3" gives me item.val without exception, the problem I face is I cannot give the line "tknid = Console.ReadLine()" after the line " int inp = Console.Read()", could it be because inp variable is used for selection in if..else statement? , I tried with breakpoints John, the issue is when I put "tknid=Console.ReadLine()" after "int inp = Console.Read()", before even getting input from the user through Console.ReadLine(), I'm getting an exception – Dularish Kuttuwa Nov 05 '16 at 09:52

1 Answers1

0

I have got the solution, the problem was not with if..else statement, it's with Console.Read().

I replaced int inp = Console.Read() with int inp = int.Parse(Console.ReadLine()) and it works fine.

Now I understand how Console.Read() works with the help of an answer I have shared below, but I'll never try to use Console.Read(), and would always use Console.ReadLine().

How Console.Read() works:

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

Community
  • 1
  • 1