473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Trapping System.NullReferenceException on start-up

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.
Oct 26 '06 #1
2 1755

Mark Raishbrook schrieb:
>
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(<CompletePathToAssemb ly>)
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 :-)

Oct 26 '06 #2
Many thanks for that, Norman. I'll give it a try and see how it goes.

"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(<CompletePathToAssemb ly>)
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 :-)
Oct 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
0
by: Matthias Kwiedor | last post by:
Hi! I want to create a WebBrowser Control at runtime. So i ported the "at start creation" of the Control to a new routine. Everything works fine, but i implement the BeforeNavigateFix and a...
17
by: Rodusa | last post by:
I am getting this exception error which is driving me nuts. System.NullReferenceException - Object reference not set to an instance of an object If I comment this line, I don't get any errors:...
2
by: fperfect13 | last post by:
Hi, I have the folowing exception Exception : System.NullReferenceException: Object reference not set to an instance of an object. 00000019 3:30:48 PM at...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
6
by: SMG | last post by:
Hi , Sory for incomplete message in last post here is the actual problem.. I am using following code in web.confiig for trapping all the error through out my site.. <customErrors mode="On"...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
1
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
0
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.