Se Br Se Br - 1 month ago 46
reST (reStructuredText) Question

JIRA REST API, JSON response, C#

I want to get issues from boards and print issues's summary (using JIRA Rest API: https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/board-getIssuesForBoard), but i don't want to get all issue's fields, - i need fields: "sprint" and "epic".

I have error:


Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object.


At line:

var response = client.RestClient.ExecuteRequest<ResponseIssues>(Method.GET,
@"/rest/agile/1.0/board/2/issue?fields='epic,sprint'");


Code (C#):

var client = Atlassian.Jira.Jira.CreateRestClient(jiraurl, login, password);
var response = client.RestClient.ExecuteRequest<ResponseIssues>(Method.GET,
@"/rest/agile/1.0/board/2/issue?fields='epic,sprint'");
foreach (var issue in response.issues)
{
Console.WriteLine(issue.fields.epic.summary);
}


Class ResponseIssues (C#):

public class ResponseIssues
{
public string expand;
public int startAt;
public int maxResults;
public int total;
public List<Issue_> issues;
}
public class Issue_
{
public string expand;
public string id;
public string self;
public string key;
public Field fields;
}
public class Field
{
public Sprint sprint;
public Epic epic;
}
public class Sprint
{
public int id;
public string self;
public string state;
public string name;
}
public class Epic
{
public int id;
public string self;
public string name;
public string summary;
public Color color;
public bool done;
}
public class Color
{
public string key;
}

Answer

It seems like something didn't get initialized properly. Whenever this happens to me, I break up the chain with assignments to try to figure out which object is null. Also catch the exception, and explore it in visual studio. In your case, try this:

try{
  var client = Atlassian.Jira.Jira.CreateRestClient(jiraurl, login, password);
  // Add breakpoint... is client null? There could be an issue with your login, password or the url you've passed. 
  // Or maybe it's Atlassian.Jira.CreateRestClient (instead of Jira.Jira.CreateRestClient), though that should get flagged by the compiler unless there's some recursion or parenting in the Jira namespace
  var restClient = client.RestClient; 
  // Add breakpoint... is restClient null? Could be that CreateRestClient is lazy-loading, in which case I'd look at the url, login and password again.
  var response = restClient.ExecuteRequest<ResponseIssues>(Method.GET, 
                   @"/rest/agile/1.0/board/2/issue?fields='epic,sprint'");
}
catch(Exception ex)
{
  Console.WriteLine(ex); // Or just breakpoint here and explore the exception.
}
Comments