Null Reference error in VB3NET2003

  • Thread starter Thread starter Paul S. Natanson
  • Start date Start date
P

Paul S. Natanson

What is a Null Reference error and how do I fix it?

My newly installed VB.Net2003 gives me a "Microsoft
Development Environment" error message box EVERY time I
try to run/start ANY project - even very simple ones.

The error says:

"An unhandled exception of
type 'System.NullReferenceException' occurred in
system.windows.forms.dll. Additional information: Object
reference not set to an instance of an object."

As best I can determine from the debugger and the help
screens, the error has something to do with de-referencing
the form. But I don't even know what that means.

This happens even when I open a project that contains
nothing more than an empty form. And reinstalling
VB.NET2003Standard did not solve the problem.

What does this error mean? What might be causing it? And
how do I fix it?

Because I doubt that I will ever be able to find this
bulletin board / news group again, I would be most
grateful if, after posting your reply here, you would also
please send it to me in an email. My email address is
(e-mail address removed) with the xxx's removed.

Thanks, Paul S. Natanson (Phone 1-908-630-0406 9 AM - 9
PM eastern USA time) (e-mail address removed)
(remove xxxx's from email address). December 22, 2003
 
Hi Paul,
What is a Null Reference error and how do I fix it?

I try to tell it on a simple way.

When you declare a simple class global in your program you do
Private myClassInProgram as myclass
But then the object (reference) does not exist.

Then you can create it everywhere with
myClassInProgram = new myclass
And the object and the reference exist in your complete program

You can also do in a Sub or a Function
dim myClassInProgram as new myclass
Then it only exist in the sub or function where it is declared and created.

I keep it with this, others I think I only confuse you, this is the basic.

I hope this helps?

Cor
 
* "Paul S. Natanson said:
My newly installed VB.Net2003 gives me a "Microsoft
Development Environment" error message box EVERY time I
try to run/start ANY project - even very simple ones.

The error says:

"An unhandled exception of
type 'System.NullReferenceException' occurred in
system.windows.forms.dll. Additional information: Object
reference not set to an instance of an object."

As best I can determine from the debugger and the help
screens, the error has something to do with de-referencing
the form. But I don't even know what that means.

This happens even when I open a project that contains
nothing more than an empty form. And reinstalling
VB.NET2003Standard did not solve the problem.

Seems to be an error in the project, not in the IDE. Does this even
occur when you create a completely new project with a single form (not
opening an existing project)? Go into the "bin" folder of your
application and start the executable file: Does this throw the
exception too? Did you have any beta or trial versions of VS.NET
installed on the machine?
 
-----Original Message-----
* "Paul S. Natanson"

Seems to be an error in the project, not in the IDE. Does this even
occur when you create a completely new project with a single form (not
opening an existing project)? Go into the "bin" folder of your
application and start the executable file: Does this throw the
exception too? Did you have any beta or trial versions of VS.NET
installed on the machine?
xxxxxxxxxxxxxxxxxxxxxx

Herfried: The problem (a "Null Reference" error) occurs
even when I create a completely new project containing
just one form - even if the form is completely blank and
empty. I get the same error msg even when I go into
my "bin" folder and run the ".exe" file. I never had any
beta or trial versions of VB.NET. But I still have Visual
Studio 6 installed on the same PC. Do you have any ideas
on what might be wrong? Paul S. Natanson (Phone 1-908-630-
0406 9 AM - 9 PM eastern USA time)
(e-mail address removed) (remove xxxx's from email
address). December 23, 2003
 
xxxxxxxxxxxxxxxxxxxxxx

Herfried: The problem (a "Null Reference" error) occurs
even when I create a completely new project containing
just one form - even if the form is completely blank and
empty. I get the same error msg even when I go into
my "bin" folder and run the ".exe" file. I never had any
beta or trial versions of VB.NET. But I still have Visual
Studio 6 installed on the same PC. Do you have any ideas
on what might be wrong? Paul S. Natanson (Phone 1-908-630-
0406 9 AM - 9 PM eastern USA time)
(e-mail address removed) (remove xxxx's from email
address). December 23, 2003

As you say it also happens whenever you start the .exe, it must be a runtime
error. Consequently you should be able to find the error in the IDE, too
(usually). When the error occurs, is there no information _where_ it occurs?
What does the callstack show?
 
Armin Zingler said:
As you say it also happens whenever you start the .exe, it must be a runtime
error. Consequently you should be able to find the error in the IDE, too
(usually). When the error occurs, is there no information _where_ it occurs?
What does the callstack show?

xxxxxxxxxxxxxxxxxxxxxxxxx

Armin: I creatd a brand new project and clicked the start button (before
writing any code). When I click the START button, the form (a blank form)
seems to load OK, but the debugger stops the program and enters BREAK mode
before showing the form.

When the program stops, I get a "Microsoft Development Environment" error
message box that says: "An unhandled exception of type
'System.NullReferenceException' occurred in system.windows.forms.dll.
Additional information: Object reference not set to an instance of an
object."

When I view the code in BREAK mode, the word "Form1" in the very first line
code is highlighted. My project has only one form (called "Form1") and it
is entirely completely blank and empty. And my project contains NO
user-written code. All I did was create a new project (called PSN) and
click the start button. The first two lines of code read as follows:

Public Class Form1
Inherits System.Windows.Forms.Form

When the error occurs, the call stack in BREAK mode shows the following:

Name: PSN.exe!PSN.Form1.Main() Line 1 + Ox1d bytes
Language: Basic

PS: I am a novice VB user trying to make the transition from VB6 to VB.NET
and I am very frustrated.

In an effort to debug this, I put a message box in the form load event and
the msg box worked OK. But as soon as I clicked OK in the msg box, I still
got the same Null Reference error.

Got any other ideas?

(e-mail address removed) (remove xxxx's from email address).
December 24, 2003
Phone 1-908-630-0406 9AM-9PM eastern USA time.
 
Paul S. Natanson said:
Armin: I creatd a brand new project and clicked the start button
(before writing any code). When I click the START button, the form
(a blank form) seems to load OK, but the debugger stops the program
and enters BREAK mode before showing the form.

When the program stops, I get a "Microsoft Development Environment"
error message box that says: "An unhandled exception of type
'System.NullReferenceException' occurred in
system.windows.forms.dll. Additional information: Object reference
not set to an instance of an object."

When I view the code in BREAK mode, the word "Form1" in the very
first line code is highlighted. My project has only one form (called
"Form1") and it is entirely completely blank and empty. And my
project contains NO user-written code. All I did was create a new
project (called PSN) and click the start button. The first two lines
of code read as follows:

Public Class Form1
Inherits System.Windows.Forms.Form

When the error occurs, the call stack in BREAK mode shows the
following:

Name: PSN.exe!PSN.Form1.Main() Line 1 + Ox1d bytes
Language: Basic

PS: I am a novice VB user trying to make the transition from VB6 to
VB.NET and I am very frustrated.

In an effort to debug this, I put a message box in the form load
event and the msg box worked OK. But as soon as I clicked OK in the
msg box, I still got the same Null Reference error.

Got any other ideas?


Paul,

in the new project, add this code to your form:

Shared Sub Main
Dim f as new Form1
application.run(f)
end sub

Start again. Does the error still occur?

You also say that Form_Load is reached, so the sub Main you mentioned above
is (perhaps) not the real error location. Could you please run the app again
and post the *full* callstack? Please make the "none user code" (or similar)
visible (by right-clicking in the callstack window and enabling "none user
code").
 
Armin Zingler said:
Paul,

in the new project, add this code to your form:

Shared Sub Main
Dim f as new Form1
application.run(f)
end sub

Start again. Does the error still occur?

You also say that Form_Load is reached, so the sub Main you mentioned above
is (perhaps) not the real error location. Could you please run the app again
and post the *full* callstack? Please make the "none user code" (or similar)
visible (by right-clicking in the callstack window and enabling "none user
code").
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Armin: I added the code that you suggested:

Shared Sub Main
Dim f as new Form1
application.run(f)
end sub

But, after clicking the START button, I still got the same Null Reference
error.

This time, when the error caused my program to enter BREAK mode, the line
that was highlighted was:

Application.Run(f)

And the full callstack (including the non-user code) was as follows:

system.windows.forms.dll!System.Windows.Forms.Control.SetVisibleCore +
0x139 bytes
system.windows.forms.dll!System.Windows.Forms.Form.SetVisibleCore + 0xf7
bytes
system.windows.forms.dll!System.Windows.Forms.Control.set_Visible + 0x18
bytes

system.windows.forms.dll!System.Windows.Forms.Application.ThreadContext.RunM
essageLoopInner + 0x162 bytes

system.windows.forms.dll!System.Windows.Forms.Application.ThreadContext.RunM
essageLoop + 0x50 bytes
system.windows.forms.dll!System.Windows.Forms.Application.Run + 0x34 bytes
PSN.exe!PSN.Form1.Main() Line 6 + 0x8 bytes Basic

PS: This time, I noticed that when the program entered break mode, a form
entitled Form1 was open. I guess that it had been there yesterday too, but
I never noticed it before because it was hidden behind my code window and
behind the error message.

I also tried adding a button to yesterday's version. But when the form
opens, I get the error before the form is populated with the button.

So, the error appears to be happening after the form loads, but before any
controls are added to it. Is that possible?

Paul S. Natanson (e-mail address removed) (For real address, just
remove the xxx's.) December 24, 2003 Phone 1-908-630-0406 (9AM-9PM
eastern USA time).

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
"Paul S. Natanson" <[email protected] (Remove xxx's for
real address.)> schrieb
Armin: I added the code that you suggested:

Shared Sub Main
Dim f as new Form1
application.run(f)
end sub

But, after clicking the START button, I still got the same Null
Reference error.

This time, when the error caused my program to enter BREAK mode, the
line that was highlighted was:

Application.Run(f)

And the full callstack (including the non-user code) was as
follows:


system.windows.forms.dll!System.Windows.Forms.Control.SetVisibleCore
+ 0x139 bytes
system.windows.forms.dll!System.Windows.Forms.Form.SetVisibleCore +
0xf7
bytes
system.windows.forms.dll!System.Windows.Forms.Control.set_Visible +
0x18
bytes

system.windows.forms.dll!System.Windows.Forms.Application.ThreadContext.RunM
essageLoopInner + 0x162 bytes

system.windows.forms.dll!System.Windows.Forms.Application.ThreadContext.RunM
essageLoop + 0x50 bytes
system.windows.forms.dll!System.Windows.Forms.Application.Run +
0x34 bytes PSN.exe!PSN.Form1.Main() Line 6 + 0x8 bytes Basic

That's really interesting. I had a look at SetVisibleCore but didn't figure
out what could cause the problem. Um...do you know how to use the
disassembly window? :) I'd single step through the disassembly to find out
what is Nothing within SetVisibleCore.
PS: This time, I noticed that when the program entered break mode, a
form entitled Form1 was open. I guess that it had been there
yesterday too, but I never noticed it before because it was hidden
behind my code window and behind the error message.

I also tried adding a button to yesterday's version. But when the
form opens, I get the error before the form is populated with the
button.

So, the error appears to be happening after the form loads, but
before any controls are added to it. Is that possible?

I guess that the form must have been made visible as you can alredy see it,
so, in Setvisiblecore, I can narrow the problem down to the native
setvisible call - still the sub is quite long.

It is hard for me to find the error. Maybe somebody else remembers the same
error (I think I've heard it before but I am not sure) and has some help for
you. I guess it's an installation (or security (or whatever)) problem.
 
Hi Paul,

I don't know the answer either,

But maybe you can make it more visible for you.

Open the designer made region and delete from this row
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

<System.Diagnostics.DebuggerStepThrough()>

Than push on the F11 function (asuming that it is your step button).

Then maybe it will become more visible.

Cor
 
Cor said:
Hi Paul,

I don't know the answer either,

But maybe you can make it more visible for you.

Open the designer made region and delete from this row
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

<System.Diagnostics.DebuggerStepThrough()>

Than push on the F11 function (asuming that it is your step button).

Then maybe it will become more visible.

Cor

xxxxxxxxxxxxxxxxxxxxxx

Cor, Armin:

I tried Cor's suggeston (above) about deleting his suggested part of the
designer-generated code, but it did not help. (Being only a novice VB6
programmer, and never having worked in VB.NET, I did not understand why Cor
asked me to remove that part of the code, but I did it anyway.) But it did
not help. I still get the same Null Reference error.

Then I created a new project and, without writing any additional code, I
began stepping through the default code which is shown below in its
entirety. The program crashed (with the usual Null Reference error) after
executing the line marked with the string of asterisks (************) shown
below.

Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub ' ***** This is where it crashed. *****
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.Text = "Form1"
End Sub
#End Region
End Class

I have already tried repairing my VB.NET installation. (That took about
three hours). If you guys don't have any more suggestions, then perhaps I
should try a complete new removal and re-install?

PS: I still have Visual Studio 6 on my PC. Could that be part of the
problem? Previoiusly, the Microsoft folks told me that my VS6 and VS.NET
should be able to peacefully co-exist on the same computer. Were they
wrong?

Thank you so very much for trying to help me.

Paul S. Natanson (Phone 1-908-630-0406 9 AM - 9PM eastern USA time)
(e-mail address removed) (remove xxxx's from email address).
December 22, 2003
 
Hi Paul,

I wrote this, so you could step through your whole program and it would not
skip the initializing part where I thought that it probably fails.

And now I saw it fails in that part, if it does not help I would do what you
said myself also, but you can give it a try again to see where it fails in
that InitializeComponent.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub ' ***** This is where it crashed. *****

Where it looks that it crashed, but it crashed in the Sub bellow, but
because of that syntax before it, it skips the debugging in that sub.

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

I hope this helps,

Cor
 
NULL REFERENCE ERROR - PROBLEM SOLVED. After several frustrating weeks,
someone at Microsoft finally figured out why I was getting a null reference
error when opening any project.

Unlike VB6 and VS6 which can handle long paths, there is apparently a limit
in VB.NET and VS.NET to how long a path can be. When I put my solutions and
projects closer to the root directory folder, the null reference error went
away.

Paul S. Natanson 1-14-04 Phone 1-908-630-0406 (9 AM - 9 PM eastern USA
time)

xxxxxxxxxxxxxxx
 
Hi Paul,

This is interresting, Herfried is always talking about a bug with long
(128char) paths in the "folderbrowserdialog" but this I never saw in this
newsgroup, can you be a little bit more precise how long paths etc?

Cor
NULL REFERENCE ERROR - PROBLEM SOLVED. After several frustrating weeks,
someone at Microsoft finally figured out why I was getting a null reference
error when opening any project.

Unlike VB6 and VS6 which can handle long paths, there is apparently a limit
in VB.NET and VS.NET to how long a path can be. When I put my solutions and
projects closer to the root directory folder, the null reference error went
away.

Paul S. Natanson 1-14-04 Phone 1-908-630-0406 (9 AM - 9 PM eastern
USA
 
Cor: The Microsoft guy that finally solved my problem did not know what the
maximum allowed path length was that would work without crashing. But after
experimenting a bit, I determined that the path should be less than about
110 characters long and less than about 7 folders deep. Note that these are
only estimates. To my surprise, I had to go through several layers of tech
support guys at Microsoft before I finally found a guy who was able to help
me. I guess that even Microsoft does not widely know about this problem. I
suggested that Microsoft should publish a Knowledge Base (KB) article on
this.

Bottom line is that to avoid a null reference error in VS.NET and/or VB.NET
you should try to avoid long paths to your projects and solutions.

Paul S. Natanson 1-15-04 Phone 1-908-630-0406 (9 AM - 9 PM eastern USA
time)

xxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
Hi Herfried,

You tell always you are using very long paths on your computer 128 and
longer, how about this?
(Not all my messages are tonight serious to you but this one is)
 
* "Cor said:
You tell always you are using very long paths on your computer 128 and
longer, how about this?

Nested paths, containing long filenames. If something is a valid path
(for Windows), controls should work with it properly too.
 
Back
Top