Part of a larger WinForms application I am making has a timer element added to the GUI. Inside the timer_tick function, I retreive data from an API, parse the elements in the returned XML-data to a string[], and then pass on the parsed array as an input to export the data to a cloud database. The parts not commented worked previously, however when I added the XML-parser and DataExport lines (which are commented in the snippet), the code returns a NullReferenceException error:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
The snippet for the timer_tick function is below:
RestClient rClient = new RestClient();
DataTransform dTransform = new DataTransform(); //new
rClient.endPoint = txtRequestURL.Text;
debugOutput("Rest Client Created.");
string strResponse = "";
strResponse = rClient.makeRequest();
string[] responseValues = new string[6]; //new
responseValues = dTransform.UnpackXML(strResponse.ToString()); //new --error appears on this line
DataExporter.ExportData(responseValues); //new
debugOutput(strResponse);
txtResponse.AppendText(Environment.NewLine);
The code for the DataTransform class:
internal class DataTransform
{
public string[] UnpackXML(string xml_string)
{
string response = xml_string;
string[] return_values = new string[6];
XmlDocument xml = new XmlDocument();
xml.LoadXml(response);
return_values[0] = xml.SelectNodes("descendant::meas[@name=\"mt1\"]")[0].InnerText.ToString();
return_values[1] = xml.SelectNodes("descendant::meas[@name=\"mt2\"]")[0].InnerText.ToString();
return_values[2] = xml.SelectNodes("descendant::meas[@name=\"mt3\"]")[0].InnerText.ToString();
return_values[3] = xml.SelectNodes("descendant::meas[@name=\"mt4\"]")[0].InnerText.ToString();
return_values[4] = xml.SelectNodes("descendant::meas[@name=\"mt5\"]")[0].InnerText.ToString();
return_values[5] = xml.SelectNodes("descendant::meas[@name=\"mt6\"]")[0].InnerText.ToString();
return return_values;
}
}
The DataExport class has been confirmed working when given a string[] array with values, so I am fairly sure it is not the instantiation here that is the issue.
Any ideas, why the NullReference appears in this case, when the function call is clearly referencing the instance of dTransform?