0

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?

1

1 Answer 1

0

I agree that is a bug but I am unsure what scenario would results in a null response but didn't throw an exception. I have submitted a pull request for a fix that brings the status code check to after it checks for a null then manually throws an exception when this scenario is encountered.

https://github.com/salesforce-marketingcloud/FuelSDK-CSharp/pull/26

1
  • Thanks for the update Mac, I have encountered Null Exception few times @ line 421. But it happens so randomly/unexpected. It is difficult to trace and figure out when and why the response would be null.
    – Ajit
    Commented Aug 1, 2016 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .