User selection to new selection - ICollection(of ElementId) Exception

User selection to new selection - ICollection(of ElementId) Exception

Anonymous
Not applicable
801 Views
3 Replies
Message 1 of 4

User selection to new selection - ICollection(of ElementId) Exception

Anonymous
Not applicable

 

Hi,

 

I am trying to create a selectionfilter via userinput based on object names instead of categories (as standard Revit does now). After a user creates a selection a form is show where al objects are listed by name. The user can select the namedobjects from which to create a new active selection. So far so good, but when I pass an elementId to tmpICollectionIds a Null Reference Exception occurs. Am I doing something wrong?

 

Dim tmpICollectionIds As ICollection(Of ElementId) = Nothing


For Each elementId As ElementId In UiSel.GetElementIds
Dim element As Element = m_Doc.GetElement(elementId)
If tmpSelectionList.Contains(element.Name.ToString) Then
tmpICollectionIds.Add(element.Id)
End If
Next

 

regards,

Raymond

0 Likes
802 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk

Dear Raymond,

 

Yes, obviously you are.

 

I cannot see the error from the code snippet you provide, though.

 

I would suggest that you look at the code excuting step by step in the debugger.

 

It is probably some simple mistake.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 4

Anonymous
Not applicable

Hi Jeremy,

 

Thank you for your response. I am completely at a loss here. Even passing the elementId's directly to another Icollection returns a null exception. The solution must be obvious, but unfortunately I am missing it. Even hardcoding an object outside of the userselection returns a null exception.

 

    Public Sub TestICollector(ByVal m_doc As Document, ByVal m_uidoc As UIDocument)

        Dim uiSel As Selection = m_uidoc.Selection
        Dim tmpICollectionIds As ICollection(Of ElementId) = Nothing

        For Each elementId As ElementId In uiSel.GetElementIds
            tmpICollectionIds.Add(elementId)
        Next

    End Sub
0 Likes
Message 4 of 4

jeremytammik
Autodesk
Autodesk

You say 

 

tmpcollection = null

 

and then you say

 

tmpcollection.Add

 

You should not be surprisedis an exception is thrown at this point.

 

If an object is null, you cannot access its methods or properties.

 

In other words, you cannot call Add on a null object.

 

I would suggest that you work through a basic .NET programming tutorial, just the first few pages, just for an hour or two, before trying anything else at all.

 

I hope this helps.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder