Trapping System.NullReferenceException on start-up

G

Guest

A client of mine is getting a NullReferenceException when starting up a small
app I've written. I've traced the cause to an unregistered DLL the app
references, so the error itself is not the problem. What I would like to do
is trap it, but I'm not sure where I should do this. Here's an excerpt from
the exception text:

System.NullReferenceException: Object reference not set to an instance of an
object.
at HDi.Form1.Form1_Load(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
[snip]

So my question is: where can I trap this error? Is there a way to test for
the presence of a DLL (and to check it's registered) before the start-up form
is created and this error is generated? Any help would be greatly appreciated.
 
N

Norman Chong

Mark said:
So my question is: where can I trap this error? Is there a way to test for
the presence of a DLL (and to check it's registered) before the start-up form
is created and this error is generated? Any help would be greatly appreciated.

Hi Mark,

If you want to do something before your forms are created, you can do
this in the 'Startup'-Event in class ApplicationEvents.vb, which shows
up when you click on 'Application' => 'View Application Events' in your
project file. But I don't know if this really solves your problem, as
I'm not sure if those event is called before creating the references...
Somehow I don't think so.
So maybe it's best when you load your dll dynamically where you need
it.
Here's the code for loading a .NET assembly ( I don't know if it's the
same when loading a COM )

Public Class LoadAssembly
Private myAssembly as Reflection.Assembly

Sub New()
' Load the assembly and handle errors ( e.g. .dll doesn't exist )
Try
myAssembly =
Reflection.Assembly.LoadFile(<CompletePathToAssembly>)
Catch ex As Exception
' Errorhandling
End Try

' Create an instance of a class from the loaded assembly
Dim tTypes() As Type = myAssembly.GetTypes()
Dim tType As Type
Dim myAssemblyObject as Object
For Each tType In tTypes
If (tType.Name.Equals(<NameOfClassYouWantToLoad>)) Then
myAssemblyObject = Activator.CreateInstance(tType, args)
End If
Next
End Sub
End Class

Hope this helps :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top