Nulll reference error on Form load.

Nulll reference error on Form load.

Anonymous
Not applicable
606 Views
3 Replies
Message 1 of 4

Nulll reference error on Form load.

Anonymous
Not applicable

Am working on an Assembly addin, this is a combination of the simpleaddin from the sample files and help from this forum that I had on a previous post:

https://forums.autodesk.com/t5/inventor-customization/my-first-addon-help/m-p/6249591#M62851

I have managed to get the addin to load the toolbar button and fire from within an assembly file, this is where I am having the issue.

Like before I am getting a null reference error on line 16 on my form's code. (See attached screen grab) But unlike before the fixes will not work.

 

This is the code I have in the project:

 

Globals.Vb:

Module Globals

    Public InvApp As Inventor.Application

End Module

 

StandardAddInServer:

Imports Inventor
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Configuration
Imports Microsoft.Win32


Namespace SimpleAddIn
    <ProgIdAttribute("SimpleAddIn.StandardAddInServer"), _
    GuidAttribute("E6EA2F87-0494-4C0D-9E05-9e71128cae77")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer
        Private WithEvents oAppEvents As ApplicationEvents
#Region "Data Members"

        Private Invapp As Inventor.Application
        'buttons
        Private m_StudboltToolButton As StudboltToolButton
        'events
        Private m_userInterfaceEvents As UserInterfaceEvents
        ' ribbon panel
        Private m_partSketchSlotRibbonPanel As RibbonPanel

#End Region

#Region "ApplicationAddInServer Members"

        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate

            Try
                'the Activate method is called by Inventor when it loads the addin
                'the AddInSiteObject provides access to the Inventor Application object
                'the FirstTime flag indicates if the addin is loaded for the first time

                'initialize AddIn members
                Invapp = addInSiteObject.Application
                Button.InventorApplication = Invapp

                'initialize event handlers
                m_userInterfaceEvents = Invapp.UserInterfaceManager.UserInterfaceEvents

                AddHandler m_userInterfaceEvents.OnResetCommandBars, AddressOf Me.UserInterfaceEvents_OnResetCommandBars
                AddHandler m_userInterfaceEvents.OnResetEnvironments, AddressOf Me.UserInterfaceEvents_OnResetEnvironments
                AddHandler m_userInterfaceEvents.OnResetRibbonInterface, AddressOf Me.UserInterfaceEvents_OnResetRibbonInterface

                'load image icons for UI items
                Dim StudboltToolImageStream As System.IO.Stream = Me.GetType().Assembly.GetManifestResourceStream("SimpleAddIn.Studbolt.ico")
                Dim StudboltToolIcon As Icon = New Icon(StudboltToolImageStream)

                'retrieve the GUID for this class
                Dim addInCLSID As GuidAttribute
                addInCLSID = CType(System.Attribute.GetCustomAttribute(GetType(StandardAddInServer), GetType(GuidAttribute)), GuidAttribute)
                Dim addInCLSIDString As String
                addInCLSIDString = "{" & addInCLSID.Value & "}"

                'create buttons
                m_StudboltToolButton = New StudboltToolButton("Studbolt Tool", "Autodesk:SimpleAddIn:StudboltToolCmdBtn", CommandTypesEnum.kShapeEditCmdType, _
                 addInCLSIDString, "Studbolt Tool", "Studbolt Tool", StudboltToolIcon, StudboltToolIcon)

                'create the command category
                Dim AssmCmdCategory As CommandCategory = Invapp.CommandManager.CommandCategories.Add("Company Assembly Tools", "Autodesk:SimpleAddIn:AssmCmdCat", addInCLSIDString)
                AssmCmdCategory.Add(m_StudboltToolButton.ButtonDefinition)

                If firstTime = True Then

                    'access user interface manager
                    Dim userInterfaceManager As UserInterfaceManager
                    userInterfaceManager = Invapp.UserInterfaceManager

                    Dim interfaceStyle As InterfaceStyleEnum
                    interfaceStyle = userInterfaceManager.InterfaceStyle

                    m_partSketchSlotRibbonPanel = Nothing

                    ' create the UI for classic interface
                    If interfaceStyle = InterfaceStyleEnum.kClassicInterface Then
                        'create toolbar
                        Dim slotCommandBar As CommandBar
                        slotCommandBar = userInterfaceManager.CommandBars.Add("Company Assembly Tools", "Autodesk:SimpleAddIn:SlotToolbar", , addInCLSIDString)

                        'add buttons to toolbar
                        slotCommandBar.Controls.AddButton(m_StudboltToolButton.ButtonDefinition)

                        'get the 2d sketch environment base object
                        Dim partSketchEnvironment As Inventor.Environment
                        partSketchEnvironment = userInterfaceManager.Environments.Item("PMxPartSketchEnvironment")

                        'make this command bar accessible in the panel menu for the 2d sketch environment
                        partSketchEnvironment.PanelBar.CommandBarList.Add(slotCommandBar)

                        'create UI for ribbon interface
                    Else
                        ' get the ribbon associated with part document
                        Dim ribbons As Ribbons
                        ribbons = userInterfaceManager.Ribbons

                        Dim partRibbon As Ribbon
                        partRibbon = ribbons.Item("Assembly")

                        ' get the tabs associated with part ribbon
                        Dim ribbonTabs As RibbonTabs
                        ribbonTabs = partRibbon.RibbonTabs

                        Dim partSketchRibbonTab As RibbonTab
                        partSketchRibbonTab = ribbonTabs.Item("id_TabDesign")

                        ' create a new panel within the tab
                        Dim ribbonPanels As RibbonPanels
                        ribbonPanels = partSketchRibbonTab.RibbonPanels

                        Dim m_partSketchSlotRibbonPanel As RibbonPanel
                        m_partSketchSlotRibbonPanel = ribbonPanels.Add("Company Assembly Tools", "Autodesk:SimpleAddIn:SlotRibbonPanel", "{E6EA2F87-0494-4C0D-9E05-9e71128cae77}", "", False)

                        ' add controls to the slot panel
                        Dim partSketchSlotRibbonPanelCtrls As CommandControls
                        partSketchSlotRibbonPanelCtrls = m_partSketchSlotRibbonPanel.CommandControls

                        ' add the buttons to the ribbon panel

                        Dim StudboltCmdCtrl As CommandControl
                        StudboltCmdCtrl = partSketchSlotRibbonPanelCtrls.AddButton(m_StudboltToolButton.ButtonDefinition, False, True, "", False)



                    End If
                End If


            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try

        End Sub

        Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate

            'the Deactivate method is called by Inventor when the AddIn is unloaded
            'the AddIn will be unloaded either manually by the user or
            'when the Inventor session is terminated

            Try
                'release objects
                RemoveHandler m_userInterfaceEvents.OnResetCommandBars, AddressOf Me.UserInterfaceEvents_OnResetCommandBars
                RemoveHandler m_userInterfaceEvents.OnResetEnvironments, AddressOf Me.UserInterfaceEvents_OnResetEnvironments

                m_StudboltToolButton = Nothing
                If Not m_partSketchSlotRibbonPanel Is Nothing Then m_partSketchSlotRibbonPanel.Delete()
                m_userInterfaceEvents = Nothing

                Marshal.ReleaseComObject(Invapp)
                Invapp = Nothing

                System.GC.WaitForPendingFinalizers()
                System.GC.Collect()

            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try

        End Sub

        Public ReadOnly Property Automation() As Object Implements Inventor.ApplicationAddInServer.Automation

            'if you want to return an interface to another client of this addin,
            'implement that interface in a class and return that class object 
            'through this property

            Get
                Return Nothing
            End Get

        End Property

        Public Sub ExecuteCommand(ByVal CommandID As Integer) Implements Inventor.ApplicationAddInServer.ExecuteCommand

            'this method was used to notify when an AddIn command was executed
            'the CommandID parameter identifies the command that was executed

            'Note:this method is now obsolete, you should use the new
            'ControlDefinition objects to implement commands, they have
            'their own event sinks to notify when the command is executed

        End Sub

        Public Sub UserInterfaceEvents_OnResetCommandBars(ByVal commandBars As ObjectsEnumerator, ByVal context As NameValueMap)

            Try
                Dim commandBar As CommandBar
                For Each commandBar In commandBars
                    If commandBar.InternalName = "Autodesk:SimpleAddIn:SlotToolbar" Then

                        'add buttons to toolbar
                        commandBar.Controls.AddButton(m_StudboltToolButton.ButtonDefinition)


                        Exit Sub
                    End If
                Next

            Catch
            End Try

        End Sub

        Public Sub UserInterfaceEvents_OnResetEnvironments(ByVal environments As ObjectsEnumerator, ByVal context As NameValueMap)

            Try
                Dim environment As Environment
                For Each environment In environments
                    'get the 2d sketch environment
                    If environment.InternalName = "PMxPartSketchEnvironment" Then

                        'make the command bar accessible in the panel menu for the 2d sketch environment
                        environment.PanelBar.CommandBarList.Add(Invapp.UserInterfaceManager.CommandBars.Item("Autodesk:SimpleAddIn:SlotToolbar"))

                        Exit Sub
                    End If
                Next
            Catch
            End Try

        End Sub

        Public Sub UserInterfaceEvents_OnResetRibbonInterface(ByVal Context As Inventor.NameValueMap)
            ' get UserInterfaceManager
            Dim userInterfaceManager As UserInterfaceManager
            userInterfaceManager = Invapp.UserInterfaceManager

            ' get the ribbon associated with part documents
            Dim ribbons As Ribbons
            ribbons = userInterfaceManager.Ribbons

            Dim partRibbon As Ribbon
            partRibbon = ribbons.Item("Assembly")

            ' get the tabs associated with part ribbon
            Dim ribbonTabs As RibbonTabs
            ribbonTabs = partRibbon.RibbonTabs

            Dim partSketchRibbonTab As RibbonTab
            partSketchRibbonTab = ribbonTabs.Item("id_TabDesign")

            ' create a new panel within the tab
            Dim ribbonPanels As RibbonPanels
            ribbonPanels = partSketchRibbonTab.RibbonPanels

            Dim partSketchSlotRibbonPanel As RibbonPanel
            partSketchSlotRibbonPanel = ribbonPanels.Add("Company Assembly Tools", "Autodesk:SimpleAddIn:SlotRibbonPanel", "{E6EA2F87-0494-4C0D-9E05-9e71128cae77}", "", False)

            ' add controls to the slot panel
            Dim partSketchSlotRibbonPanelCtrls As CommandControls
            partSketchSlotRibbonPanelCtrls = partSketchSlotRibbonPanel.CommandControls

            ' add the buttons to the ribbon panel

            Dim StudboltCmdCtrl As CommandControl
            StudboltCmdCtrl = partSketchSlotRibbonPanelCtrls.AddButton(m_StudboltToolButton.ButtonDefinition, False, True, "", False)

        End Sub
#End Region


    End Class

End Namespace

 

Form: StudboltTool

Imports Inventor
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Configuration
Imports Microsoft.Win32

Public Class StudboltTool



    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        Dim ADoc As AssemblyDocument = InvApp.ActiveDocument
      
        ' Add any initialization after the InitializeComponent() call.

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

        Dim ADoc As AssemblyDocument = InvApp.ActiveDocument
        MsgBox(ADoc.DisplayName)
    End Sub


End Class

 

As always, any help is greatly appreciated.

 

Inventor Pro 2015, SP2

Visual Studio Pro 2010

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

Owner2229
Advisor
Advisor
Accepted solution

Hi, you have the "InvApp" specifed in both Globals.Vb and StandardAddInServer.vb.

Remove it from StandardAddInServer.vb and give it a try.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 4

Anonymous
Not applicable

That did the trick.

 

Thanks a lot, you're becoming my one personal saviour on here.

 

Smiley Very Happy

0 Likes
Message 4 of 4

Owner2229
Advisor
Advisor

You're welcomed Smiley Happy

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes