错误:对象引用未初始化

发布于 2024-09-29 09:08:13 字数 564 浏览 2 评论 0 原文

我在 C# 中有以下代码:

IList> myList = null;
myList.Add(临时);

temp 是一个在其他地方贴花的字符串,并且不为空(我检查过)。我在 myList.Add(temp); 行不断收到以下错误“对象引用未初始化为对象的实例”

我在这里做错了什么???

更新问题:我已经尝试过新的
IList<字符串> myList = new List>();
正如大多数人所建议的那样,并从 Intellisense 中获取以下信息:

无法创建抽象类或接口 Systems.Collections.Generic.Ilist 的实例。

感谢您之前的回答。现在我遇到了一个特殊的问题。我的数据读取器中有一个空字符串。 (sql server 表中的 1 个字段为空。即 string.Empty)。有没有办法摆脱阅读器内部的这个问题(我的意思是 rdr )?

I have the following code in C#:

IList<string> myList = null;
myList.Add(temp);

temp is a string that is decalred elsewhere and is not null (I checked it). I keep getting the following error at the line myList.Add(temp); "Object reference not initialised to an instance of an object"

What am I doing wrong here???

Updating the quesiton: I have already tried new
IList<string> myList = new List<string>();
as most of you suggested andget the following from Intellisense:

Cannot create an instance of the abstract class or interface Systems.Collections.Generic.Ilist.

Thanks for your previous answers. Now I am running into a peculiar problem. My datareader has an empty string in it. (1 field in the sql server table is blank. that is string.Empty). Is there a way to get rid of this inside the reader (I mean rdr here)??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林散布 2024-10-06 09:08:13

您需要先初始化列表:

IList<string> myList = new List<string>();
...

请注意,在 = 右侧您必须编写 List 而不是 IList因为 IList 是一个接口,而 List 是实现该接口的类。

you need to initialize the list first:

IList<string> myList = new List<string>();
...

Please note that on the right of = you have to write List<string> not IList<string> since IList<string> is an interface, whereas List<string> is a class that implements that interface.

椒妓 2024-10-06 09:08:13

该行:

IList<string> myList = null;

不会给您一个列表,而是一个对列表可能位置的空引用。

IList<string> myList = new List<string>();

将正确实例化 myList,以便您可以使用它(添加、删除等)。

The line:

IList<string> myList = null;

does not give you a list, but an empty reference to where a list could be.

IList<string> myList = new List<string>();

would properly instantiate myList, so you can use it (Add, Remove, etc.).

幸福不弃 2024-10-06 09:08:13

在这里,看一下本教程以更好地理解变量初始化。

来自 http://www.csharphelp.com/2007/03/objects -classes-in-c/

Person Michael = new Person();

在第一行代码中,我们指定了名为age的整数变量。在第二行中,我们首先指定需要创建的对象类型,后跟对象名称,然后是名为 new 的保留运算符,最后再次键入类名,后跟括号“()”。

让我们一步步来理解它。在开头指定类名告诉 C# 编译器为该类型分配内存位置(C# 编译器知道该类的所有变量、属性和方法,因此它将分配正确的内存量)。然后我们在类名后面加上我们想要的对象变量名。其余代码”=new Person();”调用对象的构造函数。我们稍后将讨论构造函数,但现在请理解构造函数是在创建对象变量时而不是在创建对象变量之后初始化它的一种方法。例如,我们在上一节中创建的 Michael 对象可以写如下:

Person Michael = new Person(20, “Brown”);

这里我在参数列表中指定了变量的值,因此我在创建对象时初始化了变量。但为了让这段代码工作,我们需要在 Person 类中指定构造函数,我不会在这里这样做,因为构造函数部分将在后面的文章中介绍。我认为您对类和对象有了很好的介绍,我不会在下一篇文章中完成,我将讨论构造函数和构建块范围。我希望你能从我的文章中得到新的东西。

Here, take a look at this tutorial to better understand variable initialization.

From http://www.csharphelp.com/2007/03/objects-classes-in-c/

Person Michael = new Person();

In the first line of code we specified integer variablecalled age. In the second line we specified first the type of Object we need tocreate followed by the object’s name followed by a reserved operator called new and we end by typing the class name again followed byparenthesis “()”.

Let’s understand it step-by-step. Specifying the class nameat the beginning tell the C# Compiler to allocate a memory location for thattype (C# compiler knows all the variables and properties and methods of theclass so it will allocate the right amount of memory). Then we followed theclass name by out object variable name that we want it. The rest of the code”=new Person();” callthe object’s constructor. We will talk about constructor later but for nowunderstand that the constructor is a way to initialize your object’s variablewhile you are creating it not after you create it. For example, The Michaelobject we created it in the last section can be written as following :

Person Michael = new Person(20, “Brown”);

here I specified the variable’s values in the parameter listso I initialized the variables while I’m creating the object. But for this codeto work we will need to specify the constructor in the Person class and I willnot do that here because constructor section will come in later articles. Ithink you got a good introduction about Classes and Objects not I will completein in my next article and I will talk about constructors and building blockscoping. I hope you got a new thing from my article.

活雷疯 2024-10-06 09:08:13

更新问题:我已经按照大多数人的建议尝试了 new Ilist() ,并从 Intellisense 中获得了以下信息:无法创建抽象类或接口 Systems.Collections.Generic.Ilist 的实例

您无法创建接口的实例。 IList是一个接口。就像其他人在这里所说的那样,您可以使用继承该接口的具体类的实例来初始化它。注意他们如何使用 new List() 而不是 new IList();

IList<String> items = new List<String>();

Updating the quesiton: I have already tried new Ilist() as most of you suggested andget the following from Intellisense: Cannot create an instance of the abstract class or interface Systems.Collections.Generic.Ilist

You cannot create instances of an Interface. IList<T> is an interface. Like the others have said here you initialize it with an instance of a concrete class that inherits the interface. Notice how they use new List<String>() not new IList<String>();

IList<String> items = new List<String>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文