Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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# 将变量传递到另一个表单C时出现NullReferenceException_C#_Nullreferenceexception - Fatal编程技术网

C# 将变量传递到另一个表单C时出现NullReferenceException

C# 将变量传递到另一个表单C时出现NullReferenceException,c#,nullreferenceexception,C#,Nullreferenceexception,所以我有以下情况,;我想把几个变量发送到另一个表单。下面是一些代码: // In Form1 Form3 f3 = new Form3(); f3.SetVariables(pieces); // In Form3 string[] items; void SetVariables(string[] array) { items = array; } 现在这确实有效,但如果我尝试使用: items[x].Length 它会引发NullReferenceException,但如果我使用

所以我有以下情况,;我想把几个变量发送到另一个表单。下面是一些代码:

// In Form1
Form3 f3 = new Form3();
f3.SetVariables(pieces);
// In Form3
string[] items;
void SetVariables(string[] array)
{
    items = array;
}
现在这确实有效,但如果我尝试使用:

items[x].Length
它会引发NullReferenceException,但如果我使用:

String.IsNullOrEmpty(items[x]);
我正在检查项目[x]是否有值 上面的代码工作完美,没有错误。这背后有什么原因吗


谢谢

String.IsNullOrEmpty将首先检查变量是否为null。调用项[x]。当项[x]为空时,调用项[x]的长度将失败,因为没有要调用的对象。

上的长度您需要发布完整的非工作代码,包括您所有的方法和数据片段,以提供任何关于可能错误的线索。@AlbinUnnanbo片段包含我所知道的所有字符串和空项。SetVariables字面上就是将片段设置为项目。嗯,我就是这么想的。谢谢