I am using ExactTarget's Fuel SDK version .9 for c# to send emails. Sometimes (randomly) I am receiving System.NullReferenceException: Object reference not set to an instance of an object.
exception when initializing ET_Client object.
This is how I am initializing it:
NameValueCollection clientAttributes = new NameValueCollection();
clientAttributes.Add("clientId", *******);
clientAttributes.Add("clientSecret", *******);
ET_Client myclient = new ET_Client(clientAttributes);
I was trying to figure out why ET_Client initialization throws null exception. But after tracking stack trace and looking at the SDK code carefully, this is what I & my colleague found out, if you look at the code in ET_Client.cs file @ line number 418, you will see this block of code:
if (response != null)
this.Code = (int)response.StatusCode;
{
if (response.StatusCode == HttpStatusCode.OK)
{
this.Status = true;
List<ResultDetail> AllResults = new List<ResultDetail>();
if (responseFromServer.ToString().StartsWith("["))
{
JArray jsonArray = JArray.Parse(responseFromServer.ToString());
foreach (JObject obj in jsonArray)
{
APIObject currentObject = (APIObject)Activator.CreateInstance(theObject.GetType(), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new object[] { obj }, null);
ResultDetail result = new ResultDetail();
result.Object = currentObject;
AllResults.Add(result);
}
this.Results = AllResults.ToArray();
}
else
{
JObject jsonObject = JObject.Parse(responseFromServer.ToString());
ResultDetail result = new ResultDetail();
APIObject currentObject = (APIObject)Activator.CreateInstance(theObject.GetType(), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new object[] { jsonObject }, null);
result.Object = currentObject;
AllResults.Add(result);
this.Results = AllResults.ToArray();
}
}
else
{
this.Status = false;
this.Message = response.ToString();
}
}
As you can see, block of code after if (response != null) this.Code = (int)response.StatusCode; will always gets executed even if response is null.
Also, the same if-else problem is placed at two different places. @ line number 665:
if (response != null)
this.Code = (int)response.StatusCode;
{
if (response.StatusCode == HttpStatusCode.OK)
{
this.Status = true;
}
else
{
this.Status = false;
this.Message = response.ToString();
}
}
And again @ line number 938:
if (response != null)
this.Code = (int)response.StatusCode;
{
if (response.StatusCode == HttpStatusCode.OK)
{
this.Status = true;
if (responseFromServer != null)
{
JObject parsedResponse = JObject.Parse(responseFromServer);
//Check on the paging information from response
if (parsedResponse["page"] != null)
{
this.LastPageNumber = int.Parse(parsedResponse["page"].Value<string>().Trim());
int pageSize = int.Parse(parsedResponse["pageSize"].Value<string>().Trim());
int count = -1;
if (parsedResponse["count"] != null)
{
count = int.Parse(parsedResponse["count"].Value<string>().Trim());
}
else if (parsedResponse["totalCount"] != null)
{
count = int.Parse(parsedResponse["totalCount"].Value<string>().Trim());
}
if (count != -1 && (count > (this.LastPageNumber * pageSize)))
{
this.MoreResults = true;
}
}
APIObject[] getResults = new APIObject[] { };
if (parsedResponse["items"] != null)
getResults = processResults(parsedResponse["items"].ToString().Trim(), theObject.GetType());
else if (parsedResponse["entities"] != null)
getResults = processResults(parsedResponse["entities"].ToString().Trim(), theObject.GetType());
else
getResults = processResults(responseFromServer.Trim(), theObject.GetType());
this.Results = getResults.ToArray();
}
}
else
{
this.Status = false;
this.Message = response.ToString();
}
}
Here is the link to similar question, but no answer: Is this a bug in Fuel SDK library?
Can someone else also confirm the bug/error as well? Or Marketing Cloud, could you please fix this?