Undefined Object Reference for an object instance when trying to retrieve an element in an XML file

Asked

Viewed 781 times

3

I’m making a mistake tormenting myself.

Basically I am saving all equipment settings (RF Readers) in an XML file (Configs.xml). My goal is to retrieve the first element of all settings that have a "set" value (other than "") in the new attributeLote. If the recovered element is different from null I proceed with the code within the IF, otherwise I will release an exception. However, the code below gives the following error when I am trying to retrieve the element(third line): Object Reference not defined for an instance of an object.

Can anyone tell me what I’m doing wrong? Thanks!

XElement xml = XElement.Load("Configs.xml");
XElement x = null;
x = xml.Elements().Where(p => !p.Attribute("novoLote").Value.Equals("")).FirstOrDefault();
if (x != null)
{
    ...
}
else
{
    throw new ReaderNaoConfiguradoParaFuncaoException("Nenhum Reader está configurado para essa função.");
}

1 answer

2


This can be simplified to:

x = xml.Elements().FirstOrDefault(p => !p.Attribute("novoLote").Value.Equals(""));

Still, it is prone to errors. You can improve the sentence to:

x = xml.Elements().FirstOrDefault(p => p.Attribute("novoLote").Value != null && !p.Attribute("novoLote").Value.Equals(""))
  • Hello Gypsy, unfortunately did not work the two options you talked about. Both give the same error reported in my question. :(

  • Put a breakpoint on the line and check which of the elements is null.

  • Only x is null.

  • O valor armazenado em xml é o seguinte:&#xA;&#xA;<configs>&#xA; <reader port="23" ip="192.168.8.3" login="alien" senha="password" antennaSequence="0,1" power="0" tagListFormat="Custom" customFormat="TagID: ${TAGIDW} TX: ${TX} SPEED: ${SPEED}" type="alienConfig" speedFilter="0.25, -0.25" autoMode="OFF" mac="00:1B:5F:00:4E:9F" newLote="1" contamination="" changeFase="" discard="" /> </configs>

  • xml.Elements().First().Attribute("novoLote") null return?

  • Não, Ele devolve isso para x:&#xA;&#xA;<reader port="23" ip="192.168.8.3" login="alien" senha="password" antennaSequence="0,1" power="0" tagListFormat="Custom" customFormat="TagID: ${TAGIDW} TX: ${TX} SPEED: ${SPEED}" type="alienConfig" speedFilter="0.25, -0.25" autoMode="OFF" mac="00:1B:5F:00:4E:9F" newLote="1" contamination="" changeFase="" discard="" />

  • See that my question was another. My doubt is what is being null, not what is returned to x, so much so that I asked for another test in the code, as I put it above. Can you take this test, please?

  • I did so Gypsy: Xattribute a = xml. Elements(). First(). Attribute("newLote"); E returned: {newLote="1"}

  • Weird. It has something null even, but I can’t tell you what it is, since the test returned an element.

  • 1

    Hello Gypsy, I managed to solve it. Your code helped a lot. It was a small detail that I changed and worked here. Follow the resolution: x = xml.Elements(). Firstordefault(p => p.Attribute("newLote").Value != null && !p.Attribute("newLote").Value.Equals("")); I added . Value in comparison to see if it was different from null.

  • Thank you! I updated the reply.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.