Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I have a following bit of code:

C#
WebResponse Response = await ibanCalc.GetResponseAsync();

           using (var GetStream = new StreamReader(Response.GetResponseStream()))
           {
               XDocument xmlDocument = new XDocument();
               string iban = "";
               try
               {
                   xmlDocument = XDocument.Parse(GetStream.ReadToEnd());

                   if (iban != null)
                   {

                    iban = xmlDocument.Element("result").Element("iban")?.ToString();
                       return iban;

                   }


               }
               catch (NullReferenceException)
               {
                   return string.Empty;
               }


           }


Unfortunately, I cannot secure it from getting a NullReferenceException on the following line, when I try give it a null value for testing purposes:
C#
iban = xmlDocument.Element("result").Element("iban")?.ToString();


What I have tried:

I have tried to use a try catch block, using an if statement to check if value is null, or even making an iban value nullable. So far nothing seems to work. Does anyone has any ideas how to fix it?
Posted
Updated 30-Aug-21 23:12pm

First, find out which bit is null.
Replace that line with this:
var x1 = xmlDocument.Element("result");
var x2 = x1.Element("iban")'
iban = x2.ToString();
And run your code in the debugger.
Put a breakpoint on the first of those three lines, and when it hits, check the xmlDocument value. If it's null, you can look for why.
If it isn't, single step the line, and check the result. If that's null, ... you get the idea.

Find out where the null reference is and what is causing it, then you can look at why it's happening - then you can start to fix it, but not until then!
 
Share this answer
 
Comments
Member 15341738 31-Aug-21 3:23am    
Thank you for your reply. I have followed your advice and the null happens when I get to the x2 variable. In general the point of this code is to parse XML response from the server and catch if it has an iban element in it. If it doesn't, then the NullReferenceException happens. I just don't know how to change the code to prevent it from happening, I am also not very familiar with XDocument library which parses the XML.
OriginalGriff 31-Aug-21 3:36am    
So if x2 is null, that means that the xmlDocument doesn't contain a "result" element.

https://docs.microsoft.com/en-us/dotnet/api/system.xml.linq.xcontainer.element?view=net-5.0

So either work with that if that is sufficient to be sure no iban element exists, or check you have the right data in your stream at all!

I'd start by looking at the stream content to check if anything useful is in there ...
Quote:
C#
if (iban != null)
You know it's not null - you've just set it to an empty string a couple of lines before, and you haven't touched it since. This test can be removed.

Try something like this:
C#
WebResponse Response = await ibanCalc.GetResponseAsync();
using (var responseStream = Response.GetResponseStream())
{
    XDocument xmlDocument = XDocument.Load(responseStream);
    return (string)xmlDocument.Element("result")?.Element("iban");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900