0

I´m attempting to fill a POCO object but I get the NullReferenceException - Object reference not set to an instance of an object, at line "objectAreas.position.Add(objectPositions);" I think I'm not initializing well but I don't see my mistake, let's see the code:

POCO OBJECT

public class GenericQuery
{
    public sealed class Areas
    {
        public int idarea { get; set; }
        public string areaname { get; set; }
        public List<Positions> positions { get; set; }
    }

    public sealed class Positions
    {
        public int idposition { get; set; }
        public string positionname { get; set; }
    }

    public sealed class QueryAreasPositions
    {
        public int code { get; set; }
        public string response { get; set; }
        public List<Areas> areas { get; set; }
    }
}

Filling It

GenericQuery.QueryAreasPositions objectAreasPositions = new GenericQuery.QueryAreasPositions();

var query = areaRepository.Get(); //Eager Loading EntityFramework List Object, see the AreaRepository at the end

objectAreasPositions.code = 123;
objectAreasPositions.response = "anything";

foreach (var area in query)
    {
        GenericQuery.Areas objectAreas = new GenericQuery.Areas();
        objectAreas.idarea = area.IdArea;
        objectAreas.areaname = area.Name;
            foreach (var position in area.Position)
            {
                GenericQuery.Positions objectPositions = new GenericQuery.Positions();
                objectPositions.idposition = position.IdPosition;
                objectPositions.positionname = position.Name;
                ***objectAreas.position.Add(objectPositions);***//HERE
            }

        objectAreasPositions.areas.Add(objectAreas); //And maybe here
     }

AreaRepository

public List<Area> Get()
{
    using (var context = new Entities())
    {
        return context.Area.Include("Position").ToList();
    }
}

I would appreciate any help/guide you can give me, Thanks.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Dany G
  • 315
  • 2
  • 5
  • 15

2 Answers2

1

You are never initializing objectAreas.position, hence the default value for a List<T> is null.

Since you are trying to call the Add method on a null reference, you are getting a NullReferenceException.

To fix this, you should initialize the property before using it:

objectAreas.position = new List<GenericQuery.Positions>();

Alternatively, you can add this logic on GenericQuery.Areas constructor, which would be more appropriate:

public sealed class Areas
{
    public int idarea { get; set; }
    public string areaname { get; set; }
    public List<Positions> positions { get; set; }

    public class Areas()
    {
         positions = new List<Positions>();
    }
}
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
0

Shouldn't you rather be doing like below. Your position is null cause not yet initialized and thus the said exception.

objectAreas.position = new List<Position>();
objectAreas.position.Add(objectPositions);
Rahul
  • 76,197
  • 13
  • 71
  • 125