且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

System.NullReferenceException发生在

更新时间:2022-03-19 08:22:06

Andreas,

你可以使用非创建的对象。

例如


MyClass myobject;

myobject.MyMethod();

- 错误;


你应该这样做:


MyClass myobject;

myobject = new MyClass( );

myobject.MyMethod();


"AndreasWirén" <一个***** @ clan.dcs.se> ó???Yéì/ó???Yéìá×??×?ó??èóì????Y ??:

新闻:HB *********** ********* @ newsc.telia.net ...
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" <an*****@clan.dcs.se> ó???Yéì/ó???Yéìá × ??×?ó??è óì???àY??:
news:HB********************@newsc.telia.net...
你好我是C#.NET新手做一个小型学校项目但是我得到了一个/ >奇怪的错误信息。

'System.NullReferenceException''发生在system.windows.forms.dll
附加信息:对象引用未设置为
对象的实例。

正如我的参考文献看起来我似乎在使用旧框架(1.0.3705)这个,但重新引用更新的框架(1.1.4322)dll'产生
同样的错误。在我尝试
编译完全托管项目的所有情况下都会发生这种情况。即使从头开始尝试只编译空表格也会发生这种情况。

如果您阅读我已经包含的旧主题,您可以看到他更改了他的
源路径整个项目是没有放在
MyDocuments文件夹中的东西。我做了完全相同的事情并且有效,我的问题是为什么?所有其他应用程序(到目前为止)编译完美无瑕,只有这些C#Windows应用程序项目失败。

问候,

AndreasWirén>
操作系统:Windows XP Professional SP1,包含所有最新的修补程序和更新。

使用System;
使用System.Drawing;
使用System.Collections;
使用System.ComponentModel;
使用System.Windows.Forms;
使用System.Data;

命名空间WindowsApplication1
{
///< ;摘要>
/// Form1的摘要说明。
///< / summary>
公共类Form1:System.Windows.Forms.Form
{
///< summary>
///所需的设计变量。
///< / summary>
私有System.ComponentModel.Container components = null;
{
//
// Windows窗体设计器支持需要
//
InitializeComponent();

//
// TODO:添加任何con InitializeComponent调用后的结构代码
//
}
///< summary>
///清理正在使用的所有资源。
/ //< / summary>
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components!= null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#region Windows表单设计器生成的代码
///< summary>
///支持Designer的必需方法 - 不要使用代码编辑器修改此方法的内容。
///< / 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>
///主要切入点对于应用程序。
///< / summary>
[STAThread]
static void Main()
{/> Application.Run(new Form1()) ;
}

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

}
}
}

http://groups.google.se/groups?hl = sv ... 3D%2522Applica
tion.Run(new%2BForm1())%253B%2522%2B%2522object%2B reference%2Bnot%2Bset%2522
%26hl% 3Dsv%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D6FADa.54216%2524g33.74 9173%2540weber.videotron.net%26rnum%3D6
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=sv...3D%2522Applica tion.Run(new%2BForm1())%253B%2522%2B%2522object%2B reference%2Bnot%2Bset%2522 %26hl%3Dsv%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D6FADa.54216%2524g33.74 9173%2540weber.videotron.net%26rnum%3D6



Dmitry Karneyev< ka ****** @ msn.com&gt ;写道:
Dmitry Karneyev <ka******@msn.com> wrote:
Andreas,
你可以使用非创建的对象。


MyClass myobject;
myobject.MyMethod();
- 错误;

你应该这样做:

MyClass myobject;
myobject = new MyClass();
myobject.MyMethod( );
Andreas,
You may use non created object.
for example

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

You shoul do:

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




但这并不是这里发生的事情。代码编译并运行正常。

请注意,根据消息,错误发生在

编译期间,而不是在运行时。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


它似乎从system.windows.forms.dll中给出NullReferenceException


如果使用Visual Studio 2002,则不能使用v1。 1.


我会尝试重新安装框架。


-

使用M2,Opera革命性的电子邮件客户端: http://www.opera.com/m2/
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.

--
Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/