Search code examples
c#trello.net

Got a "NullReferenceException was unhandled" exception while working with Trello


I am currently working with TrelloNet to develop my own Trello organizer application. Started with two piece of codes shown below ("xxxx" is my own authentication code and application key, masked for privacy purpose):

using System;
using TrelloNet;

class Program
{
    static void Main(string[] args)
    {
        ITrello trello = new Trello("xxxx");
        trello.Authorize("xxxx");

        var myBoard = trello.Boards.Add("My Board");
    }
}

This code worked, a "My Board" board was successfully added. But the second slightly modified code met an exception.

using System;
using TrelloNet;

class Program
{
    static void Main(string[] args)
    {
        ITrello trello = new Trello("xxxx");
        trello.Authorize("xxxx");

        Member me = trello.Members.Me();
        Console.WriteLine(me.FullName);       //exception poped up here.
    }
}

After execution, there came an exception poped out at the line Console.WriteLine(me.FullName);, the exception is

  • NullReferenceException was unhandled.
  • An unhandled exception of type 'System.NullReferenceException' occurred in TrelloOrganizer.exe Additional information: Object reference not set to an instance of an object.

object trello is indeed an instance of an object, why did I get this exception?

Thanks


Solution

  • You're trying to get the FullName property of the me object, which is null. Ensure that that object is set prior to using it. Or check for null after getting the reference to it. You may have to work back from here. I don't know what TrelloNet does when you try to Authorize and it fails. If it can return null from the Me() call, then you should check that it's not null first before using it. In any event, you should have some error handling in there too using a try-catch construct.