对象引用未设置为对象EDIT的实例

max*_*esa 1 c# nullreferenceexception

我是新来的,我尝试查看旧问题,但我也是c#的新手,我发现很难解决下面的问题:

if (File.Exists(@"C:\ESC\Impostazioni.txt"))
{
    TextReader lettore_file = new StreamReader(@"C:\ESC\Impostazioni.txt");
    String opzioni = lettore_file.ReadLine();

    int i;
    for (i = 0; i < opzioni.Length; i++) <----here, indicating "i=0"
    {
        if (opzioni[i] == '-')
        {
             char[] coloregenerale = new char[i];
             for (int j = 0; j < i; j++)
               coloregenerale[j] = opzioni[j];

           break;
Run Code Online (Sandbox Code Playgroud)

Kar*_*son 6

在尝试循环遍历每个字符之前,您应该检查字符串值是null还是空,如下所示:

if(!String.IsNullOrEmpty(opzioni))
{
    // Put loop through character logic here
}
Run Code Online (Sandbox Code Playgroud)