0

I made a program which to show LTE device information. The problem i faced is when trying to display only device name, this error occurs " Object reference not set to an instance of an object."

xml

 public void GetInfo()
    {
        Initialise();

        var Info = Get("api/device/information");

        LogMessage(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
        LogMessage(string.Format("Device name:\n {0}", Info.SelectSingleNode("//response/devicename").InnerText)); //something wrong here?

    }

Error :

System.NullReferenceException: Object reference not set to an instance of an object.
   at HuaweiRouterTool.HuaweiRouter.GetInfo() in C:\Users\pxc\Desktop\HuaweiRouterTool-master\HuaweiRouter.cs:line 233
   at HuaweiRouterTool.Program.DeviceInfo() in C:\Users\pxc\Desktop\HuaweiRouterTool-master\Program.cs:line 41
   at HuaweiRouterTool.Program.Main(String[] args) in C:\Users\pxc\Desktop\HuaweiRouterTool-master\Program.cs:line 21

But when i delete "devicename", it displayed all value.

public void GetInfo()
    {
        Initialise();

        var Info = Get("api/device/information");

        LogMessage(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
        LogMessage(string.Format("Device name:\n {0}", Info.SelectSingleNode("//response").InnerText));

    }

Result

what am i doing wrong here?

neoniles
  • 41
  • 7
  • 1
    It would be helpful if you could post your Error Message as Text, rather than image. – Anu Viswan May 13 '19 at 16:32
  • If the node doesn't exist then InnerText will be null causing the error. You should first search for the node and check if null before checking the InnerText – jdweng May 13 '19 at 16:39
  • @jdweng if InnerText is null `string.Format` can handle it and just produce `"Device name:\n "` string. It looks like a `SelectSingleNode` returns null. But when running on xml from question this method should return a value – Aleks Andreev May 13 '19 at 16:47
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dour High Arch May 13 '19 at 16:54
  • Node is exist. I tried with other api path, used same code and it displayed the value correctly. weird – neoniles May 13 '19 at 16:55

1 Answers1

0

I solved this already.

Instead of "devicename" I changed to "DeviceName" and it works now.

neoniles
  • 41
  • 7