|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Trapping System.NullReferenceException on start-upA 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. Mark Raishbrook schrieb:
> Hi Mark,> 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. > 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 :-) Many thanks for that, Norman. I'll give it a try and see how it goes.
Show quoteHide quote "Norman Chong" wrote: > > 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 :-) |
|||||||||||||||||||||||