Question

Why do I get a NullReferenceException here?

I also tried to use a static constructor but that didn't help either. When I put a breakpoint within the method I only see the "Command c" variables but not the variables for this static class.

static class TableManager
{
    public static Dictionary<Guid, Table.AbstractTable> Tables = new Dictionary<Guid, AbstractTable>();

    public static bool CreateTable(Command c)
    {
        if (!Tables.ContainsKey(c.Table.TableID))
        {
            //Do magic
            return true
        }
        else
        {
            //Table already exists, return false.
            return false;
        }
    }
}
Was it helpful?

Solution

 public static bool CreateTable(Command c)
 {
        if(c != null && c.Table != null)
        {
             if (!Tables.ContainsKey(c.Table.TableID))
             {
                 //Do magic
                 return true
             }
             else
             {
                //Table already exists, return false.
                return false;
             }
         }
         else
         {
             //Ouch!! c or c.Table are null man
         }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top