NotifyIcon program - Button exception

  • Thread starter Thread starter bole2cant
  • Start date Start date
B

bole2cant

When I add a Button to my NotifyIcon program I get the following:

An unhandled exception of type 'System.NullReferenceException' occurred in
unknown module.

Additional information: Object reference not set to an instance of an object.

I suspect this is a common novice error but I haven't any idea what I need to
do to get this to work. "Help" seems to be geared toward people who have been
coding for a couple of years!

The code I have added is:

Public Class Form1
<snip of all code down to last Sub>

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Me.Close()
End Sub
End Class

Any suggestions?
 
If I delete the Sub I don't get the exception error.
I'll have to look up what a try/catch is. Thanks for the reply.

-Doug

=================
"William Ryan eMVP" wrote in a message:
 
A try catch block is the structured way to handle/trap exceptions. As far
as that sub, something's weird. me is VB.NET's equivalent of 'this' in C
langauges and all you are calling is Close on it. So everythign is
compiling fine you jsut get a NullReference if you leave this in? Hmm, also
missing a Handles.Clause. Go ahead and kill this code then add it again
using the ide's visual tools. It will shell out the event for you. Then
just add code (me.Close());.

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
Can you post the full code?
I've got a feeling you might not be unloading the NotifyIcon properly before
closing the form??
Just a thought... expert please :D
________________________________
The Grim Reaper
 
Grim Reaper,

Ah, you may be onto something. You are correct in your suggestion that I am not
unloading the Icon from the Notification tray. I sometimes have 3 or 4
instances of the icon, but eventually they go away. <g>

I have tried unloading niRedLight but cannot get the code right,
e.g., niRedLight.Dispose or niRedLight.Icon.Dispose, but that doesn't fix it.
(There is no Close option.)

How do I unload it?

As for posting the full code. Is that acceptable here? And it wouldn't show how
my properties are set....

I could post it on my web site or make it available as a download--the whole
project (or is the correct term Solution!)
What's your preference?

You may have noticed that I am also having a problem with displaying the
context menu. (A different thread)

Thanks for your reply.

-Doug

====================================================
"The Grim Reaper" wrote in a message:
 
I have just discovered what is giving me the exception error. It is NOT the
code, it is the fact that the button is on the form. If I delete the code and
leave the button on the form I still get the error message.

I decided to make the code available on my download web page.

http://www.xmission.com/~sherwin/download2.html

Question: Are all these (7) files necessary to enable someone else to download
and run my program? Also, I am concerned that the icons may not be contained in
the files I have made available. I guess any icon you have would work okay?

-Doug

=============================
in a message:
 
bole2cant said:
I have just discovered what is giving me the exception error. It is
NOT the code, it is the fact that the button is on the form. If I
delete the code and leave the button on the form I still get the
error message.

I decided to make the code available on my download web page.

http://www.xmission.com/~sherwin/download2.html


Maybe I missed something, but...
a) I don't get an exception
b) btnClose_Click is not an event handler because "Handles..." is missing.
c) you don't need to call niRedLight.Icon.Dispose() because the NotifyIcon
is a component, and a component is disposed automatically when the Form is
closed and therefore disposed.
Question: Are all these (7) files necessary to enable someone else to
download and run my program? Also, I am concerned that the icons may
not be contained in the files I have made available. I guess any icon
you have would work okay?

If you select the icon in the designer, it is stored in form1.resx and
compiled as a resource in the executable.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
in a message:

Maybe I missed something, but...
a) I don't get an exception
b) btnClose_Click is not an event handler because "Handles..." is missing.
c) you don't need to call niRedLight.Icon.Dispose() because the NotifyIcon
is a component, and a component is disposed automatically when the Form is
closed and therefore disposed.
===================================

Armin,

Thanks for your reply and for taking the time to run my program. From your list
above....

a) What I hear you saying that you ran my code without making any changes
and you didn't get the exception error db. How can that be explained? A
different version of VB.NET? (I am using VB.NET standard edition.) A setting I
need to change in ???? (setup options?)

Did the context menu work for you, Armin? (Right-clicking on niRedLight
icon....)

b) I am too much of a novice to understand ....

c) Understood. I will get rid of that code.

Does anyone using standard edition get the exception?

-Doug
 
bole2cant said:
in a message:
===================================

Armin,

Thanks for your reply and for taking the time to run my program. From
your list above....

a) What I hear you saying that you ran my code without making any
changes and you didn't get the exception error db. How can that be
explained? A different version of VB.NET? (I am using VB.NET standard
edition.) A setting I need to change in ???? (setup options?)

Did the context menu work for you, Armin? (Right-clicking on
niRedLight icon....)

Umm... no, I didn't use the context menu. I've read the thread again but
didn't find that the error occurs when the context menu is opened - or where
does the error occur? No I had a look at the contextmneu, but it doesn't
have items. Is this intended?
b) I am too much of a novice to understand ....

Private Sub btnClose_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs)

You have to add "Handles" to have the procedure handle the click event of
the button:

Private Sub btnClose_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnClose.Click


c) Understood. I will get rid of that code.

Does anyone using standard edition get the exception?

I'm using VS 2003 Prof., WinXP Prof.

The most important thing is to tell us where and when the exception occurs
and what the message of the exception says.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Back
Top