系统.NullReferenceException对象引用未设置为对象的实例.我+ +

本文关键字:实例 对象 NullReferenceException 对象引用 设置 系统 | 更新日期: 2023-09-27 18:08:40

我想从result[]到问题每隔两个变量读取一次。

string[] questionstr = null;
int ii = 0;
for (int i = 0; result.Length > i;)
{
  questionstr[ii] = result[i];
  ii = ii+1;
  i = i+2;
}

它给了我System.NullReferenceExceptionii=ii+1;我也试过ii++;但同样的错误。

系统.NullReferenceException对象引用未设置为对象的实例.我+ +

您的NullReferenceException必须来自上面的行:

questionstr[ii] = result[i];

数组questionStr没有初始化。尝试使用它会导致异常。

你应该在使用它之前初始化它,像这样:

string[] questionStr = new string[result.Length];

使数组的大小足以容纳所有的结果

您必须按如下方式初始化字符串数组questionstr:

 var questionStr = new string[result.Length/2+1];