0

I have some code which creates many dictionaries to creates JSON data and then send it as API request. The first time this works, this is the dictionary:

var values = new Dictionary<string, string>
{
  { "TestrunId", testrunId },
  { "Sequence", "1" },
  { "Type", "RequirementSet" },
};

new QualityMonitorApi().SendRequest(values);

Then the second time I do this:

var values = new Dictionary<string, string>
{
  { "TestrunId", testrunId },
  { "Sequence", "1" },
  { "Type", "Requirement" },
  { "Name", jsonRequirementItem.Name },
  { "UniqueId", requirement.UniqueId },
};

new QualityMonitorApi().SendRequest(values);

But then I get a System.NullReferenceException was unhandled error with the info: The object reference is not set to an instance of an object. So what am I doing wrong here?

Hille
  • 2,123
  • 22
  • 39
John
  • 6,404
  • 14
  • 54
  • 106
  • 1
    You should inspect `jsonRequirementItem.Name` and `requirement.UniqueId` and `new QualityMonitorApi()` to see if there NULL or not. – Royi Namir Oct 26 '17 at 09:19
  • Well you obviously get a NullReferenceException. Which means, you should check all your objects you pass into the dictionary. I see a lot ob objects which might cause this problem. – ckruczek Oct 26 '17 at 09:19
  • Read the duplicate carefully and follow the instructions. I can guarantee without any doubt at all that you will find the cause of your problem. – ProgrammingLlama Oct 26 '17 at 09:21
  • 1
    Offtopic: I updated your formatting. Put four spaces in front of your code (at least four spaces in every line), which creates a nicer code block. Usage of the ` character should stay limited to `inline usage` of code, where it mixes with normal text. – Flater Oct 26 '17 at 09:22
  • OK, I will check the objects for null values, thanks – John Oct 26 '17 at 09:23
  • the solution is easy, check for nulls before you initialize your dictionaries as @Royi Namir said, but also ensure that you initialize testrunId, because `string` (System.String) can be `null` too, btw it would be great if you share the whole class that contains this dictionary initializate, at least the constructor and properties initialization – Ferus7 Oct 26 '17 at 09:23

0 Answers0