Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么在向列表框添加ArrayList元素时会出现空引用异常?_C#_Arraylist_Listbox_Compact Framework_Windows Ce - Fatal编程技术网

C# 为什么在向列表框添加ArrayList元素时会出现空引用异常?

C# 为什么在向列表框添加ArrayList元素时会出现空引用异常?,c#,arraylist,listbox,compact-framework,windows-ce,C#,Arraylist,Listbox,Compact Framework,Windows Ce,此代码在注释行中出现“空引用异常”: …向我展示我的期望(在这两种情况下,正如预期的那样): 我还在作业中添加了一个“ToString”,因此现在: listBoxCommandsSent.Items.Add(arrLst[i].ToString()); …但无效。您似乎没有初始化listBoxCommandsSent或listBoxCommandsSent.Items。您可以添加 if(listBoxCommandsSent==null) MessageBox.Show("listB

此代码在注释行中出现“空引用异常”:

…向我展示我的期望(在这两种情况下,正如预期的那样):

我还在作业中添加了一个“ToString”,因此现在:

listBoxCommandsSent.Items.Add(arrLst[i].ToString());

…但无效。

您似乎没有初始化
listBoxCommandsSent
listBoxCommandsSent.Items
。您可以添加

if(listBoxCommandsSent==null)
    MessageBox.Show("listBoxCommandsSent is null");
if(listBoxCommandsSent.Items==null)
    MessageBox.Show("Items is null");

检查什么是空的。

是否有可能是
listBoxCommandsSent
空的?检查
listBoxCommandsSent
是否为空以及
listBoxCommandsSent.Items
是否为非空将是首先要检查的两个项目。您可以显示您在哪里初始化
arrLst
以及在哪里初始化吗
listBoxCommandsSent
至于您的编辑,问题不可能是您要添加的项目。即使它们是
null
,它也不会抛出该异常
listBoxCommandsSent
是什么
null
。我不明白listbox怎么可能是null。很明显,我在这里遗漏了一些东西。根据他对每个MessageBox正在做的事情的描述,我认为
listBoxCommandsSent
为null/displayingarrList[0]=null不应导致listBoxCommandsSent.Items.Add(arrLst[I])中出现null引用异常;行。列表框的
Items
属性永远不会为空,并且不需要由列表框的用户初始化。但是listbox本身可能是空的。我不知道listBoxCommandsSent的类型是什么。它没有显示在他的代码中。@KirillBestemyanov名字
listBox
是一个非常明显的线索,他在描述OP中发生的事情时将其称为“列表框”。
arrLst element 0 is ! 0 200 200 210 1
arrLst element 0 from i is ! 0 200 200 210 1
listBoxCommandsSent.Items.Add(arrLst[i].ToString());
if(listBoxCommandsSent==null)
    MessageBox.Show("listBoxCommandsSent is null");
if(listBoxCommandsSent.Items==null)
    MessageBox.Show("Items is null");