System.NullReferenceException occurred in

A

Andreas Wirén

Hi I'm a C# .NET newbie doing a minor school project but I'm getting a
strange error message.

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

As my references look I seem to be using the old framework(1.0.3705) for
this, but re-referencing to the newer framework(1.1.4322) dll's produces the
same error. This happens under all circumstances when I'm trying to compile
a fully managed project. Even from scratch when trying to just compile the
empty form this happens.

If you read the old thread I've included you can see that he changed his
sourcepath for the whole project to something that is not put in the
MyDocuments folders. I did exactly the same thing and it worked, the
question from me is WHY? All other apps (so far) compiles flawless, it's
only these C# Windows Applications Projects that fail.

Regards,

Andreas Wirén

OS : Windows XP Professional SP1 with all the recent hotfixes and updates.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}

http://groups.google.se/groups?hl=s...ideotron.net&rnum=6&prev=/groups?q=%22Applica
tion.Run(new%2BForm1())%253B%2522%2B%2522object%2Breference%2Bnot%2Bset%2522
%26hl%3Dsv%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D6FADa.54216%2524g33.74
9173%2540weber.videotron.net%26rnum%3D6
 
D

Dmitry Karneyev

Andreas,
You may use non created object.
for example

MyClass myobject;
myobject.MyMethod();
-- error;

You shoul do:

MyClass myobject;
myobject = new MyClass();
myobject.MyMethod();

Andreas Wirén said:
Hi I'm a C# .NET newbie doing a minor school project but I'm getting a
strange error message.

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

As my references look I seem to be using the old framework(1.0.3705) for
this, but re-referencing to the newer framework(1.1.4322) dll's produces the
same error. This happens under all circumstances when I'm trying to compile
a fully managed project. Even from scratch when trying to just compile the
empty form this happens.

If you read the old thread I've included you can see that he changed his
sourcepath for the whole project to something that is not put in the
MyDocuments folders. I did exactly the same thing and it worked, the
question from me is WHY? All other apps (so far) compiles flawless, it's
only these C# Windows Applications Projects that fail.

Regards,

Andreas Wirén

OS : Windows XP Professional SP1 with all the recent hotfixes and updates.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

}
}
}

http://groups.google.se/groups?hl=s...=&ie=UTF-8&oe=UTF-8&selm=6FADa.54216%24g33.74
9173%2540weber.videotron.net%26rnum%3D6
 
J

Jon Skeet [C# MVP]

Dmitry Karneyev said:
Andreas,
You may use non created object.
for example

MyClass myobject;
myobject.MyMethod();
-- error;

You shoul do:

MyClass myobject;
myobject = new MyClass();
myobject.MyMethod();

But that's not what's happening here. The code compiles and runs fine.
Note that according to the message, the error occurs during
compilation, not at runtime.
 
M

Morten Wennevik

It appears to give a NullReferenceException from system.windows.forms.dll

If you use Visual Studio 2002 you can't use v1.1.

I'd try to reinstall the framework.
 
A

Andreas Wirén

Hmm, I'm not sure if I know what the difference between runtime and
compilation is. Anyway, after pressing F5 the program launches but "locks"
and the debugger steps in and gives the error message. This is already a
fresh installation of VS.NET. All I've done is a few(4-5) VB apps.

And I really doubt it's an code error as someone said, the error occurs on
this machine I'm on right now, but not on another box, running exactly the
same VS release coupled with the exact same code I posted earlier.

My guess is that something stupid has happened with some of the DLL files or
some other linked files used by the compiler. The other box (that the
compile works on) has a much fresher install of XP (with all hotfixes and
updates) but does not have the SP1 of VS.NET installed.

"Morten Wennevik" <[email protected]> skrev i meddelandet
It appears to give a NullReferenceException from system.windows.forms.dll

If you use Visual Studio 2002 you can't use v1.1.

I'd try to reinstall the framework.
 
J

Jon Skeet [C# MVP]

Andreas Wirén said:
Hmm, I'm not sure if I know what the difference between runtime and
compilation is.

If the error occurs when you compile the code, it's a compile-time
error.

If the error occurs when you run the code, it's a run-time error.
Anyway, after pressing F5 the program launches but "locks"
and the debugger steps in and gives the error message. This is already a
fresh installation of VS.NET. All I've done is a few(4-5) VB apps.

Ah - that's a run-time error. (When you say about "the compile works" I
inferred that it was a compile-time error.)
And I really doubt it's an code error as someone said, the error occurs on
this machine I'm on right now, but not on another box, running exactly the
same VS release coupled with the exact same code I posted earlier.

My guess is that something stupid has happened with some of the DLL filesor
some other linked files used by the compiler. The other box (that the
compile works on) has a much fresher install of XP (with all hotfixes and
updates) but does not have the SP1 of VS.NET installed.

It doesn't sound like it's a compiler error. What happens if you
compile it on the "working" machine and then copy the executable which
is produced onto the "broken" machine and run it there?
 
A

Andreas Wirén

The file works flawless when compiled and executed on the "broken" machine.
I'm also not certain if it really goes into runtime, all that happens when I
press F5 is that the debugger points to a line in the code:

static void Main()
{

Application.Run(new Form1());

}<-----

and a messagebox pops up with the error message System.NullReference...

The form do appear in the taskbar though but when clicked it's only white.
Still the only solution to this problem seem to be to move all my project
files to some other folder on the drive, but that seems like a really silly
solution :)


"Jon Skeet [C# MVP]" <[email protected]> skrev i meddelandet
Andreas Wirén said:
Hmm, I'm not sure if I know what the difference between runtime and
compilation is.

If the error occurs when you compile the code, it's a compile-time
error.

If the error occurs when you run the code, it's a run-time error.
Anyway, after pressing F5 the program launches but "locks"
and the debugger steps in and gives the error message. This is already a
fresh installation of VS.NET. All I've done is a few(4-5) VB apps.

Ah - that's a run-time error. (When you say about "the compile works" I
inferred that it was a compile-time error.)
And I really doubt it's an code error as someone said, the error occurs on
this machine I'm on right now, but not on another box, running exactly the
same VS release coupled with the exact same code I posted earlier.

My guess is that something stupid has happened with some of the DLL files or
some other linked files used by the compiler. The other box (that the
compile works on) has a much fresher install of XP (with all hotfixes and
updates) but does not have the SP1 of VS.NET installed.

It doesn't sound like it's a compiler error. What happens if you
compile it on the "working" machine and then copy the executable which
is produced onto the "broken" machine and run it there?
 
J

Jon Skeet [C# MVP]

Andreas Wirén said:
The file works flawless when compiled and executed on the "broken" machine.

Eh? Do you mean when it's compiled elsewhere and then executed on the
"broken" machine? If it works fine when compiler *and* executed on the
"broken" machine, I can't see what's wrong at all!
I'm also not certain if it really goes into runtime, all that happens when I
press F5 is that the debugger points to a line in the code:

static void Main()
{

Application.Run(new Form1());

}<-----

and a messagebox pops up with the error message System.NullReference...

That's runtime. If it only breaks when you run it, it's runtime.
The form do appear in the taskbar though but when clicked it's only white..
Still the only solution to this problem seem to be to move all my project
files to some other folder on the drive, but that seems like a really silly
solution :)

Okay, I'm very confused as to what's provoking this now. Could you give
a brief summary of when it works and when it doesn't, on which machine,
etc?
 
A

Andreas Wirén

Sorry if I was confusing you :)

Machine1 (not the broken one) : Compile - OK! , Executing - OK!
Machine2 (the broken one) : Compile - Error!, Executing - Error!

Machine2 (the broken one) while using the binary from Machine1: Executing -
OK!

Hope that clear things out.

Thanks for your time btw! It's not really that big issue but it's annoying
not being able to save documents/projects to whereever you want.
"Jon Skeet [C# MVP]" <[email protected]> skrev i meddelandet
Andreas Wirén said:
The file works flawless when compiled and executed on the "broken"
machine.

Eh? Do you mean when it's compiled elsewhere and then executed on the
"broken" machine? If it works fine when compiler *and* executed on the
"broken" machine, I can't see what's wrong at all!
I'm also not certain if it really goes into runtime, all that happens when I
press F5 is that the debugger points to a line in the code:

static void Main()
{

Application.Run(new Form1());

}<-----

and a messagebox pops up with the error message System.NullReference...

That's runtime. If it only breaks when you run it, it's runtime.
The form do appear in the taskbar though but when clicked it's only white.
Still the only solution to this problem seem to be to move all my project
files to some other folder on the drive, but that seems like a really silly
solution :)

Okay, I'm very confused as to what's provoking this now. Could you give
a brief summary of when it works and when it doesn't, on which machine,
etc?
 
J

Jon Skeet [C# MVP]

Andreas Wirén said:
Sorry if I was confusing you :)

Machine1 (not the broken one) : Compile - OK! , Executing - OK!
Machine2 (the broken one) : Compile - Error!, Executing - Error!

When you say "Compile - Error!" are you now saying that you *do* get an
error at compile-time?
Thanks for your time btw! It's not really that big issue but it's annoying
not being able to save documents/projects to whereever you want.

It's also *only* when you save the project in My Documents somewhere
that you get the problem, yes?

It does sound highly bizarre to me.

Out of interest, what happens when you have the project in My Documents
but compile the source code from the command line?
 
A

Andreas Wirén

With "Compile - Error!" I mean it's not giving me an error-free compile. The
..exe file is still created and I suppose that means it's an run-time error.
However, I found something very funny, if I just move the file (the "not
error free exe") down one folder in the hierarchy (using Explorer) the file
executes flawless.

Files on Machine1:

c:\documents and settings\Andreas.ATHLON1\My Documents\Visual Studio
Projects\WindowsApplication3\bin\Debug\test1.exe
c:\documents and settings\Andreas.ATHLON1\My Documents\Visual Studio
Projects\WindowsApplication3\bin\Debug\test2.exe

test1.exe is compiled on Machine1 without any errors, test2 is compiled on
Machine2 but with errors.
test1.exe can be executed anywhere on Machine1 but it gives me an exception
on Machine2 when executed from the above specific dir.
test2.exe works in the exact same mysterious ways as test1.exe but is
compiled with errors as I said earlier.

But if I just move any of the two exe files to another dir, say
c:\documents and settings\Andreas.ATHLON1\My Documents\Visual Studio
Projects\WindowsApplication3\bin\test1.exe

Then the file executes without a hitch :p

Haha, this sure is a mess but I hope you understand.

"Jon Skeet [C# MVP]" <[email protected]> skrev i meddelandet
Andreas Wirén said:
Sorry if I was confusing you :)

Machine1 (not the broken one) : Compile - OK! , Executing - OK!
Machine2 (the broken one) : Compile - Error!, Executing - Error!

When you say "Compile - Error!" are you now saying that you *do* get an
error at compile-time?
Thanks for your time btw! It's not really that big issue but it's annoying
not being able to save documents/projects to whereever you want.

It's also *only* when you save the project in My Documents somewhere
that you get the problem, yes?

It does sound highly bizarre to me.

Out of interest, what happens when you have the project in My Documents
but compile the source code from the command line?
 
J

Jon Skeet [C# MVP]

Andreas Wirén said:
With "Compile - Error!" I mean it's not giving me an error-free compile.

So it's giving you an error at compile-time? What error is that?
The .exe file is still created and I suppose that means it's an run-time error.
However, I found something very funny, if I just move the file (the "not
error free exe") down one folder in the hierarchy (using Explorer) the file
executes flawless.

Okay, that's very strange - I'm entirely baffled now, I'm afraid :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top