C# System.ArgumentNullException:值不能为null。参数名称:String。请参阅堆栈跟踪的输出

C# System.ArgumentNullException:值不能为null。参数名称:String。请参阅堆栈跟踪的输出,c#,null,C#,Null,这里完全没有人,还在努力学习C#。。。不确定引发此异常的位置。我没有在输出中看到它,程序编译和运行都完美无缺 编辑:我所说的完美是指代码编译、运行、做它需要做的事情,并在不崩溃的情况下正确退出 EDIT2:更多信息:这是一个在线类,我没有看到visual studio中引发的异常,但当我将代码粘贴到他们的“现场答案”块中,并让它检查工作时,它引发了异常,这使得查找源代码变得困难。VS中的输出窗口未返回ArgumentNullException static void Main()

这里完全没有人,还在努力学习C#。。。不确定引发此异常的位置。我没有在输出中看到它,程序编译和运行都完美无缺

编辑:我所说的完美是指代码编译、运行、做它需要做的事情,并在不崩溃的情况下正确退出

EDIT2:更多信息:这是一个在线类,我没有看到visual studio中引发的异常,但当我将代码粘贴到他们的“现场答案”块中,并让它检查工作时,它引发了异常,这使得查找源代码变得困难。VS中的输出窗口未返回ArgumentNullException

    static void Main()
    {
        Console.Write("Enter the number of times to print \"Yay!\": ");
        while (true)
        {
            try
            {
                string times = Console.ReadLine();
                int repeater = int.Parse(times);
                while (repeater > 0)
                {
                    Console.WriteLine("Yay!");
                    repeater--;
                }
                break;
            }
            catch (FormatException)
            {
                Console.WriteLine("You must enter a whole number!");
            }
        }
        Console.ReadLine();
    }
EDIT3:终于从学校的编译器那里得到了这个错误:

System.ArgumentNullException: Value cannot be null.
Parameter name: String
  at System.Number.StringToNumber (System.String str, NumberStyles options, System.NumberBuffer& number, System.Globalization.NumberFormatInfo info, Boolean parseDecimal) [0x00054] in /builddir/build/BUILD/mono-4.4.2/external/referencesource/mscorlib/system/number.cs:1074 
  at System.Number.ParseInt32 (System.String s, NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /builddir/build/BUILD/mono-4.4.2/external/referencesource/mscorlib/system/number.cs:745 
  at System.Int32.Parse (System.String s) [0x00000] in /builddir/build/BUILD/mono-4.4.2/external/referencesource/mscorlib/system/int32.cs:120 
  at Treehouse.CodeChallenges.Program.Main () <0x41076f10 + 0x0004e> in :0 
  at MonoTester.Run () [0x00197] in MonoTester.cs:125 
  at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:28 
System.ArgumentNullException:值不能为null。
参数名称:字符串
在/builddir/build/build/mono-4.4.2/external/referencesource/mscorlib/System/Number.cs中的System.Number.StringToNumber(System.String str,NumberStyles选项,System.NumberBuffer&Number,System.Globalization.NumberFormatInfo,Boolean parseDecimal)[0x00054]
在/builddir/build/build/mono-4.4.2/external/referencesource/mscorlib/System/Number.cs:745中的System.Number.ParseInt32(System.String s,NumberStyles样式,System.Globalization.NumberFormatInfo-info)[0x00014]处
在/builddir/build/build/mono-4.4.2/external/referencesource/mscorlib/System/Int32.cs:120中的System.Int32.Parse(System.String s)[0x00000]处
在0中的Treehouse.codecchallenges.Program.Main()处
在MonoTester.cs中的MonoTester.Run()[0x00197]处:125
在MonoTester.cs中的MonoTester.Main(System.String[]args)[0x00013]处:28
练习说明:如果用户输入的是十进制或非数字,则通过打印“必须输入整数”将输入验证添加到程序中。提示:捕获FormatException。

尝试:

    static void Main()
    {
        Console.Write("Enter the number of times to print \"Yay!\": ");
        while (true)
        {
            try
            {
                string times = Console.ReadLine();
                int repeater = int.Parse(times);
                while (repeater > 0)
                {
                    Console.WriteLine("Yay!");
                    repeater--;
                }
                break;
            }
            catch (FormatException)
            {
                Console.WriteLine("You must enter a whole number!");
            }
        }
        Console.ReadLine();
    }
if (times != null) {
   int repeater = int.Parse(times);
   // rest of your code here
} else {
   // deal with the null, you could just break and ask for more input
}
替换:

int repeater = int.Parse(times);
与:


可能是
int.Parse(times)
失败,因为times为空。发布堆栈跟踪以获取更多帮助。

我想花点时间感谢大家对我的问题所做的贡献。它确实帮助我找到了一个“正确”的答案。。。对于那些对此感兴趣的人来说,学校的编译器最终接受了这段代码作为“正确答案”。它不是一个编写良好的程序,当抛出异常时(特别是这个问题涉及的异常),程序就会退出。我真的不知道如何让它在试图处理null时再次提示用户输入,但事实就是这样,项目完成了,我的代码被排除在外。如果你想提供一些见解,请随意

static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            while (true)
            {
                string times = Console.ReadLine();
                if (times != null)
                {
                    try
                    {
                        int repeater = int.Parse(times);
                        if(repeater < 0)
                        {
                            Console.WriteLine("You must enter a positive number.");
                            return;
                        }
                        else
                        {
                            while (repeater > 0)
                            {
                                Console.WriteLine("Yay!");
                                repeater--;
                            }
                            break;
                        }
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("You must enter a whole number!");
                    }
                }
                else
                {
                    return;
                }
            }
            Console.ReadLine();
        }
static void Main()
{
Console.Write(“输入打印\“耶!\”:)的次数”;
while(true)
{
字符串时间=Console.ReadLine();
如果(次!=null)
{
尝试
{
int repeater=int.Parse(次);
中频(中继器<0)
{
WriteLine(“您必须输入一个正数。”);
返回;
}
其他的
{
而(中继器>0)
{
控制台。WriteLine(“耶!”);
中继器--;
}
打破
}
}
捕获(格式化异常)
{
WriteLine(“您必须输入一个整数!”);
}
}
其他的
{
返回;
}
}
Console.ReadLine();
}

如果传递null,将抛出ArgumentNullException。我不知道如果仍在抛出这些类型的异常,您是否可以说应用程序运行正常……调试器肯定会准确地告诉您这发生在哪一行以及问题是什么?我的意思是,拜托,这太基本了,太愚蠢了,现在不太想写一个答案,但你可能想看看
Int32.TryParse
…重现问题的唯一方法是在输入端键入CTRL+Z,否则,据我所知,此代码无法生成该异常。请阅读有关格式的抱歉Liam。在上面的代码中,如果没有解析,转发器将为0,代码将继续。另外-我建议不要使用while(true)。Liam-感谢链接。。。我想知道如何做到这一点。利亚姆-你因为格式问题(我正在学习发布答案)而把我拒之门外,但我给出了最好的答案。这似乎不公平。在我的解决方案中,代码继续运行,然后按预期中断。感谢您的帮助。这个答案同样可以成功编译并运行,但不会修复愚蠢的类编译器所看到的错误。@A.Sellite,你确定吗?您问题中的异常堆栈跟踪清楚地表明,当
时间
空时,异常来自
int.Parse(times)
。当您正确地使用dahui的解决方案时,实际上不可能仍然发生这种异常。也许课程系统在修复后抛出了一个不同的异常?也许,我会再次检查,但我已经提交了我的坏程序,它例外,所以我不能回去检查。。。我通过VS运行了这段代码,它运行得很好。我觉得我明白了修复程序是如何起作用的,所以这给了我一个未来使用的工具!:)我认为这个解决方案是可以的。代码可以重新构造为更易于阅读(如果您需要帮助,请查看),但在
时间
空时退出程序是有意义的。考虑到这只能在用户发生时发生。