Form Button Object reference not set

Form Button Object reference not set

ERGCONSULT
Explorer Explorer
1,356 Views
6 Replies
Message 1 of 7

Form Button Object reference not set

ERGCONSULT
Explorer
Explorer

I have written some code to create a project file based on information on a form the original code was VB6 and sense I upgraded it to VB10  when I click the cmdCreat button I get

System.NullReferenceException: Object reference not set to an instance of an object.
at CreateProjII.frmCreateProj.cmdCreate_Click(Object sender, EventArgs e)  

 

Here is the form code

 

Option Explicit On

Imports Inventor
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Linq


Public Class frmCreateProj

Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
Me.Close()
End Sub

Public Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click

 

Dim oApp As Inventor.Application
oApp = ThisApplication

Dim oDocs As Inventor.Documents
oDocs = oApp.Documents

Dim filename As String
If txtFileNum.Text = "" Then
filename = txtProj.Text & "_" & txtComp.Text
Else
filename = txtFileNum.Text
End If


Dim IpjName As String
IpjName = txtIpjName.Text

Dim projnum As String
projnum = txtProj.Text

Dim compname As String
compname = txtComp.Text

Dim Title As String
Title = txtTitle.Text

Dim vendor As String
vendor = "Gerbcon"

'create project folders
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename)
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Templates")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Content Center Files")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Assemblies")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Parts")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Drawings")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\SentItems")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Quotes")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Imported components")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Notes")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Electrical")
My.Computer.FileSystem.CreateDirectory("H:\Current Engineering\" & filename & "\Drawings" \ " txtProj.txt-1")


'Copy the Iparts folder and rename it Standard Parts
My.Computer.FileSystem.CopyDirectory("C:\NewProject\Iparts2010", "H:\Current Engineering\" & filename & "\StandardParts")
My.Computer.FileSystem.CopyDirectory("C:\NewProject\BOM", "H:\Current Engineering\" & filename & "\BOMS")

 

 

'generate ProjInfo.xml inside Templates folder
Dim B As XElement = _
<Project>
<FileNumber><%= filename %></FileNumber>
<SONumber><%= projnum %></SONumber>
<Customer><%= compname %></Customer>
<JobTitle><%= Title %></JobTitle>
</Project>


B.Add(<AdditionalSOs>
<AddSO1><%= projnum %></AddSO1>
</AdditionalSOs>) 'create element to throw additional SO numbers into starting with current SO
B.Add(<AdditionalJobTitles>
<AddJobTitle1><%= Title %></AddJobTitle1>
</AdditionalJobTitles>) 'create element to throw additional Titles into starting with current title
B.Save("H:\Current Engineering\" & filename & "\Templates\ProjInfo.xml")

'Finally, copy and modify new .ipj file (project file)
My.Computer.FileSystem.CopyFile("C:\NewProject\Temp2017.ipj", "H:\Current Engineering\" & filename & "\" & IpjName & ".ipj")

 

MsgBox("PROJECT BUILD COMPLETE", vbOKOnly)
Me.Close()


End Sub

 

End Class

E Gerber
0 Likes
Accepted solutions (1)
1,357 Views
6 Replies
Replies (6)
Message 2 of 7

BKSpurgeon
Collaborator
Collaborator

Hi there

 

When ever you see something like this - it means that the pertinent reference (in this case cmdCreat) is not pointing to an actual button/or object. 

 

Where have you created the button? Where is your UI code?

 

You need to instantiate the button somewhere in your code otherwise you will get the above error.

 

e.g.

 

System.Windows.Forms.Button cmdCreat  = new System.Windows.Forms.Button();

 

0 Likes
Message 3 of 7

ERGCONSULT
Explorer
Explorer

First off Thank you for taking the time to look at this,

There is nothing in the project anywhere resembling your comments and I believe all the UI is located in the StandardAddInServer.vb which is attached.

My apologies if I am not clear I am fairly new at this and I have only been maintaining the code for the past few years,

 

E Gerber
0 Likes
Message 4 of 7

ActivistInvestor
Mentor
Mentor

@Anonymous wrote:

First off Thank you for taking the time to look at this,

There is nothing in the project anywhere resembling your comments and I believe all the UI is located in the StandardAddInServer.vb which is attached.

My apologies if I am not clear I am fairly new at this and I have only been maintaining the code for the past few years,

 


 

Your button click event handler where the exception is being thrown references variables that appear nowhere in the code you posted.

 

You are expecting others here to tell you what's wrong with code that you didn't post.

 

Try running your code in the debugger and step through the code in the event handler to find out what variable has not been initialized.

0 Likes
Message 5 of 7

ERGCONSULT
Explorer
Explorer

I have attached the complete project in the hopes that you can point out what is missing, Here is a how I went about creating this project, I created a new project using the  2018 wizard I then added the old cod and form to the project using the add existing item feature, I then modified the code to display the button on the UI to open the form, Which works fine. If I click the cancel button the form closes without error, 

I hope this is more useful than my previous posts 

Thank you in advance for taking the time to guide me 

E Gerber
0 Likes
Message 6 of 7

ERGCONSULT
Explorer
Explorer

Please disregard my previous post I attached the original project not the  current one 

Current project attached to this post

E Gerber
0 Likes
Message 7 of 7

ActivistInvestor
Mentor
Mentor
Accepted solution

@Anonymous wrote:

I have attached the complete project in the hopes that you can point out what is missing, Here is a how I went about creating this project, I created a new project using the  2018 wizard I then added the old cod and form to the project using the add existing item feature, I then modified the code to display the button on the UI to open the form, Which works fine. If I click the cancel button the form closes without error, 

I hope this is more useful than my previous posts 

Thank you in advance for taking the time to guide me 


Your project is an Inventor plug-in.  This is the AutoCAD .NET programming forum.

 

There is a forum for Inventor customization, which is where you should be seeking help. 

 

Regardless of what kind of project it is, to find out where the problem is, You have to

run the code in the debugger. Ask in the above forum how to go about debugging

Inventor plug-ins, if you're not sure how to do that.