Null Reference Exception

Null Reference Exception

jpchaisson
Advocate Advocate
886 Views
7 Replies
Message 1 of 8

Null Reference Exception

jpchaisson
Advocate
Advocate

its been a few years since ive gotten my hands dirty with writing code for Inventor and im getting an error when i test a few lines of code and i cant figure out why. if i open old projects and run the code everything works. so i have a Drawing open in Inventor and i have two custom iProperties on the drawing that is blank at the moment and i have a small vb.net UI with a button, when i click the button im trying to populate the custom iProperties with values hard coded just as a test to get the code correct. Line 4 throws me a Null Reference Exception. can anyone explain what im missing?

 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


Dim oStation As String = "Test"
Dim oStationID As String = "Test ID"

 

Dim oApp As Inventor.Application = GetObject(, "Inventor.Application")
Dim oDoc As DrawingDocument = oApp.ActiveDocument     '<-------Problem Line-----


Dim oCustomiProp As PropertySet
oCustomiProp = oDoc.PropertySets.Item("Inventor User Defined Properties")

oCustomiProp.Item("Station").Value = oStation

End Sub

0 Likes
887 Views
7 Replies
Replies (7)
Message 2 of 8

dalton98
Collaborator
Collaborator
Dim oObj As String = "Inventor.Application"
Dim oApp As Inventor.Application = GetObject(, oObj)
Dim oDoc As DrawingDocument
oDoc = oApp.ActiveDocument
0 Likes
Message 3 of 8

jpchaisson
Advocate
Advocate

Still getting a Null Reference.... i am really confused this is such a simple line of code and ive never gotten this before.

 

jpchaisson_0-1652930224346.png

 

0 Likes
Message 4 of 8

dalton98
Collaborator
Collaborator

I'm not familiary with VBA, but I believe you need to write 'Set' in front of new objects. This is an example from the api helper. It can be found by clicking the drop down for the (?) in the top right > Help > Programming/API help.

Public Sub CreateCustomTable()
    ' Set a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument

 

0 Likes
Message 5 of 8

jpchaisson
Advocate
Advocate

This is VB.NET not VBA

 

Ive never had this problem before. its driving me crazy. everything looks correct

0 Likes
Message 6 of 8

dalton98
Collaborator
Collaborator
0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

Hi @jpchaisson.  It is possible to have the Inventor application open, without actually having any documents open.  Could that possibly be the case here?  Then there is the situation where you might have multiple instances of Inventor open...one with 1 or more documents open, and the other with no documents open, and maybe your code is grabbing the instance without any open documents.  Just throwing some stuff out there.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

JelteDeJong
Mentor
Mentor

try replacing the line:

Dim oApp As Inventor.Application = GetObject(, "Inventor.Application")

with the following:

Dim oApp As Inventor.Application =  System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes