Background
I have a Winform application
(VB.NET
framework 4.0). Within the application are two forms: frmHome (startup form) and frmdePDF (secondary form with an Adobe PDF Reader
COM
control). frmHome grabs a PDF pathway from DFS, checks if the pathway actually exists, and if so, loads the file into my PDF control and shows frmdePDF. Now, this works 100% of the time on my local machine. However, the initial load on any other machine throws a NullReferenceException
. Other events (e.g. my datagridview
CellContentClick
) that call the SAME code works fine on all machines. I understand (I hope correctly) that the issue is initializing that frmdePDF the first time.
Code
The code that loads the datagridview
(dgDE) and pulls the PDF pathway:
Private Sub btnDELegalLOMCState_Click(sender As Object, e As EventArgs) Handles btnDELegalLOMCState.Click
loadStatus = "State"
Dim deState As String = Nothing
''Validate and set state
If Me.cbDEState.SelectedIndex = 0 Then
MessageBox.Show("Please select a state.")
Exit Sub
Else
deState = Me.cbDEState.Text
End If
''Populate dgDE/set data bindings
Try
Me.cbDERegion.ComboBox.SelectedIndex = -1
Me.dgDE.DataSource = Nothing
Me.SpSelectLOMAbyStateTableAdapter.Fill(Me.DevGISDataSet.spSelectLOMAbyState, 4, deState)
Me.dgDE.DataSource = Me.SpSelectLOMAbyStateBindingSource
Me.bnDE.BindingSource = Me.SpSelectLOMAbyStateBindingSource
Me.txtDEIssueDte.DataBindings.Clear()
Me.txtDECaseNum.DataBindings.Clear()
Me.txtDECommNum.DataBindings.Clear()
Me.txtDEIssueDte.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.SpSelectLOMAbyStateBindingSource, "IssueDte", True))
Me.txtDECaseNum.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.SpSelectLOMAbyStateBindingSource, "CaseNum", True))
Me.txtDECommNum.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.SpSelectLOMAbyStateBindingSource, "CommNum", True))
Catch ex As Exception
MessageBox.Show("Error loading LOMAs: " & ex.Message.ToString)
End Try
''Load PDF
If Me.dgDE.RowCount <> 0 Then
Dim i As Integer = Me.dgDE.CurrentRow.Index
Dim pathdePDF As String = Me.dgDE.Rows.Item(i).Cells(5).Value.ToString
LoaddePDF(pathdePDF)
End If
End Sub
The Public Sub
that contains code to load the file and show frmdePDF:
Public Sub LoaddePDT(ByVal pathdePDF As String)
If System.IO.File.Exists(pathdePDF) Then
frmdePDF.pdfLOMC.LoadFile(pathdePDF)
frmdePDF.Show()
Else
MessageBox.Show("Image not available. Please check FEMA and CAMSIS.")
frmdePDF.Hide()
End If
End Sub
Other things I have tried
Creating a new instance of the form each time:
Public Sub LoadPDF_test(ByVal pathdePDF As String)
Dim openForms = Application.OpenForms.OfType(Of frmdePDF)()
While openForms.Any
openForms.First.Close()
End While
If System.IO.File.Exists(pathdePDF) Then
Dim newfrmdePDF As New frmdePDF
newfrmdePDF.pdfLOMC.LoadFile(pathdePDF)
newfrmdePDF.Show()
Else
MessageBox.Show("Image not available. Please check FEMA and CAMSIS.")
End If
End Sub
This code works on my machine 100%. It behaves the same way as the first load code on any other machine (throws the NullReferenceException
).
Exception Text
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at SaveLOMC.frmHome.LoadPDF_test(String testPath) in C:\Data\Alycia\Development\LOMC\Projects\TFS_test\SaveLOMC\SaveLOMC\frmHome.vb:l ine 46
at SaveLOMC.frmHome.btnDENewLOMCState_Click(Object sender, EventArgs e) in C:\Data\Alycia\Development\LOMC\Projects\TFS_test\SaveLOMC\SaveLOMC\frmHome.vb:l ine 440
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I have tried stepping through the debugger on my local machine. But again, I can't recreate the error on it so the objects all have assigned values.
I know this question has been asked endlessly. I have read through all of these (plus MORE!):
What is a NullReferenceException, and how do I fix it?
"Object reference not set to an instance of an object?"
Object reference not set to an instance of an object. vb.net looking up a string
visual basic: object reference not set to an instance of an object
Visual Basic - Object reference not set to an instance of an object
VB.Net How to set an object reference to an instance of an object?
object reference not set to an instance of an object. vb.net for some but not all code
Object reference not set to an instance of an object.
specific error on :"Object reference not set to an instance of an object."
Object reference not set to an instance of an object
"Object reference not set to an instance of an object" message
object reference not set to an instance of an object error showing
Additional information: my local machine is 64 bit, the other machines are 32 bit. The associated .dll files for the Adobe component are 32 bit. My Target CPU in the Application Properties is x86.
Any advice is appreciated, I wouldn't ask this broad question if I hadn't exhausted my options.
Solution
There was nothing wrong with the form load event--I was trying to manually set the location/bounds of the form on load. Set the bounds to a non-existent screen (my computer has three screens--everyone else has 2). Learning experience?