ISelectionFilter NullReferenceException when debugging

ISelectionFilter NullReferenceException when debugging

Anonymous
Not applicable
686 Views
2 Replies
Message 1 of 3

ISelectionFilter NullReferenceException when debugging

Anonymous
Not applicable

Hello everyone

 

In a lot of my commands in my plugin, I'm using an ISelectionFilter to control what elements the user can select, and it all works fine in the live versions, but I'm getting an NullReferenceException before I select anything in debug mode on the following line: 

 

 
return (e.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_RoomTags));

 

in this class:

 

        public class RoomPickFilter : ISelectionFilter
        {
            public bool AllowElement(Element e)
        {     
                return (e.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_RoomTags));
        }

            public bool AllowReference(Reference r, XYZ p)
            {
                return false;
            }
        }

 

 

Am I missing something? 

 

I'm wondering how to handle this, because it never returns and error in the live projects. 

 

0 Likes
Accepted solutions (1)
687 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni
Accepted solution

Yes, I think you are missing something. 

 

Afaik, the element Category property can be null. In that case, the Id property call will throw an exception.

 

You can add a check for null in a separate step, e.g.:

 

Category cat = e.Category;
return null != cat &&
  cat.Id.IntegerValue.Equals((int)BuiltInCategory.OST_RoomTags));

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

Anonymous
Not applicable

Thank you @jeremy_tammik 

 

That works perfectly! (As always)

I accepted your answer the solution.

 

 

0 Likes