Hi @sanil_heartbeats. Since you said the error says object ref not set to an instance of an object, and looking at that last example code, I see two opportunities for that to happen. In Line 2, the variable named 'invAttribSet' may not have a value set to it, meaning it may have been 'declared', and may have had its Type set, but may not have had a value given to it yet. If that was true, then that would be where the error is happening. The other place is in Line 7, with the variable named 'attribShape', where we have the same exact issue. Since I assume Line 2 is the only place you are attempting to set that variable's value, it will only get a value set to it 'IF' the Try portion succeeds, but will not get a value set to it if the Try portion fails, and it goes to the Catch section. So, when the Attribute was not found in the 'Try' portion, it is like the Try portion never happened, and no value was set to that variable, then the Catch portion runs, where it still does not set a value to that variable. The 'Finally' portion will always run, no matter if the initial Try worked, or if the Catch portion ran OK. So, the Finally section would be trying to use a variable that has no value, causing the error.
A Try...Catch...Finally...End Try statement does have limitations, and they can be a bit complicated to understand at first, but they can also be extremely helpful, if used correctly. For instance, they can have multiple Catch sections, but the code being used within each Catch section should be different enough that if they 'failed' (caused an Exception), that Exception would be a different Type of Exception (caused an error for a different type of reason).
https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finall...
When dealing with Attributes in Inventor, there are some built-in systems for checking if the ones you are looking for exist or not. The main one is the AttributeManager, which is right under the Document object (Document.AttributeManager). That has multiple tools for finding anything within that document that has attributes associated with them. Next, the AttributeSets object and the AttributeSet object both have the property named 'NameIsUsed', which gives us a way to check if a specifically named AttributeSet or Attribute is already within that AttributeSets collection or AttributeSet collection, which won't throw an error. Below is an example of correctly utilizing those 'NameIsUsed' properties, and avoiding having to use Try...Catch...End Try statements.
Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
If oPDoc Is Nothing Then Return
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
Dim oAttSet As Inventor.AttributeSet = Nothing
Dim oAtt As Inventor.Attribute = Nothing
Dim sAttVal As String = ""
If oPDef.AttributeSets.NameIsUsed("NewIPropertyEditor") Then
oAttSet = oPDef.AttributeSets.Item("NewIPropertyEditor")
Logger.Info("Found the specified AttributeSet")
If oAttSet.NameIsUsed("Shape") Then
oAtt = oAttSet.Item("Shape")
Logger.Info("Found the specified Attribute")
If oAtt.ValueType = ValueTypeEnum.kStringType Then
sAttVal = oAtt.Value
Logger.Info("Attribute Value = " & sAttVal)
End If
End If
End If
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)