By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

NotifyIcon program - Button exception

P: n/a
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?
--
-Doug
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Use a try/catch around the lines that you think are causing this. I really
doubt this is the line causing the problem.

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
"bole2cant" <sh*****@xxxmission.com> wrote in message
news:40***********************@news.xmission.com.. .
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?
--
-Doug

Nov 20 '05 #2

P: n/a
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/
"bole2cant" <sh*****@xxxmission.com> wrote in message
news:40***********************@news.xmission.com.. .
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:
Use a try/catch around the lines that you think are causing this. I really doubt this is the line causing the problem.

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/

==============================
"bole2cant" wrote in a message:
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?


Nov 20 '05 #3

P: n/a
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

"bole2cant" <sh*****@xxxmission.com> wrote in message
news:40***********************@news.xmission.com.. .
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?
--
-Doug

Nov 20 '05 #4

P: n/a
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:
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
============================================= "bole2cant" wrote in a message:

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?


Nov 20 '05 #5

P: n/a
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

=============================
"bole2cant" wrote in a message:
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:
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


Nov 20 '05 #6

P: n/a
"bole2cant" <sh*****@xxxmission.com> schrieb
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

Nov 20 '05 #7

P: n/a

"Armin Zingler" wrote in a message:
"bole2cant" 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
exception error message.

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

http://www.xmission.com/~sherwin/download2.html
<snip>


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

Nov 20 '05 #8

P: n/a
"bole2cant" <sh*****@xxxmission.com> schrieb

"Armin Zingler" wrote in a message:
"bole2cant" 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 exception error message.

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

http://www.xmission.com/~sherwin/download2.html
<snip>
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....)


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

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.