简体   繁体   中英

Linq Enumerable gives NullReferenceException in IE 9. Why?

Am developing a web-application in ASP.NET (C#) and following code snippet is part of a function:

string[] paramList = new string[2];
string[] paramValList = new string[2];

XDocument doc = XDocument.Parse(paramXML);
var oParamCollection = from parameters in doc.Descendants("myfunction")
          select new
          {
          Name = parameters.Attribute("myParam").Value,
          Value = parameters.Attribute("myParamValue").Value
          };

int i=0;
foreach (var oParam in oParamCollection)
{
    string name = oParam.Name.Trim();
    string value = oParam.Value.Trim();

    paramList[i] = oParam.Name.Trim();
    paramValList[i++] = oParam.Value.Trim();

    if (i >= 2)
    break;
}

Following is the value of paramXML variable (parameter) being used at the 3 code-line:

<root><myfunction myparam=" DeliveryDate" myparamvalue="2012-07-01" ></myfunction></root>

Now the issue is that when I open my web-application in Chrome or Firefox, there is not error but when I use it in IE 9 (in same state and with same development environment), I get System.NullReferenceException: Object reference not set to an instance of an object at the start of foreach loop. While debugging, I checked the value of oParamCollection in QuickWatch while using in Chrome or FireFox and I get the count as 1 but when I checked the value while using the application in IE 9, the count gives the exception. Am so much confused. Have spent quite sometime on internet searching for this problem but no pointer till now. SO is my last hope. Thank you in advance.

I suspect that for some reason when using IE9, you don't have those attributes in all cases. (You say you've presented the value of paramXML , but I strongly suspect it isn't the same when the client is IE9 as it is in the working cases. That's the only reason your code would fail.)

You can hide the problem by casting XAttribute to string instead of using the Value property:

var oParamCollection = from parameters in doc.Descendants("spparam")
                       select new
                       {
                           Name = (string) parameters.Attribute("myParam"),
                           Value = (string) parameters.Attribute("myParamValue")
                       };

Now you won't get an exception... but you'll have to cope with having null values for Name or Value if the corresponding attribute isn't present.

After lot of debugging, finally I have found the solution. The problem is surely in the code snippet (due to my silly mistake) mentioned in the question. The problematic code is as follows:

            var oParams = from parameters in l_oXDoc.Descendants("myfunction")
                          select new
                          {
                              //Name = (string) parameters.Attribute("myParam"),
                              //Value = (string) parameters.Attribute("myParamValue")
                              Name = parameters.Attribute("myParam").Value,
                              Value = parameters.Attribute("myParamValue").Value
                          };

The issue was that the XML string had the attribute names myParam and myParamValue in lowercase whereas here in code I was checking in camel case. The problem got resolved the moment I changed it to 'myparam' and 'myparamvalue' into lower case in my code. My rectified code is as follows:

            var oParams = from parameters in l_oXDoc.Descendants("myfunction")
                          select new
                          {
                              Name = parameters.Attribute("myparam").Value,
                              Value = parameters.Attribute("myparamvalue").Value
                          };

But my doubt still remains the same, why it happens only for IE and not while using Chrome or FF? I request respectable members of SO to try this and check it on their own.

I would like to thank Jon Skeet for giving me a pointer (+1 for you). @Jon, it my case, the Value property works for attribute. If you have any reference to cases where the Value may not work, please do share it with me. Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM