2

I have the following code snippet:

var familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));
var doorsCategoryfilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
var doorInstancesFilter = new LogicalAndFilter(familyInstanceFilter, doorsCategoryfilter);
List<Element> doors = new FilteredElementCollector(doc)
    .WherePasses(doorInstancesFilter)
    .ToList();
FamilyInstance doorFI = (FamilyInstance)doors[0];
ElementId wallid = doorFI.Symbol.get_Parameter(BuiltInParameter.HOST_ID_PARAM)
    .AsElementId();

where I try to obtain the ID of the wall containing a door. However if I run this code then the line ElementId wallid =doorFI.Symbol.get_Parameter(BuiltInParameter.HOST_ID_PARAM).AsElementId(); throws a null pointer exception. I have checked that the door in question is indeed inside a wall and should therfor have a parent object.

The test scenario I'm runing is a simple 2 rooms with 1 door between them test case.

Note that I know what a null reference expcetion is, I know doorFI.Symbol.get_Parameter(BuiltInParameter.HOST_ID_PARAM) returns null, I want to know how/why it returns null. If anything is still unclear feel free to leave a comment so I can improve this question.

Thijser
  • 2,358
  • 1
  • 31
  • 62
  • Shouldn't `(FamilyInstance)door[0];` be `(FamilyInstance)doors[0];` ? – Martin Oct 19 '15 at 11:32
  • Yes you are correct I changed it. – Thijser Oct 19 '15 at 11:33
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – HimBromBeere Oct 19 '15 at 12:03
  • No it's not, I know what a nullreferenceException is and how it's caused, obviously Symbol.get_Parameter(BuiltInParameter.HOST_ID_PARAM) is null but I want to know why/how it;s null when doors require a parrent object. As seen by the fact that @Augusto Goncalves was able to answer this question in a way that would make 0 sense in the question of which this is supposed to be a duplicate. – Thijser Oct 19 '15 at 12:10

1 Answers1

4

I believe you should not need the Symbol:

doorFI.get_Parameter(BuiltInParameter.HOST_ID_PARAM).AsElementId();
Augusto Goncalves
  • 8,071
  • 2
  • 12
  • 38