gpt4 book ai didi

c# - 如何修复 int.Parse 中的 ArgumentNullException?

转载 作者:行者123 更新时间:2023-11-30 21:56:12 25 4
gpt4 key购买 nike

这是在 Mono 中运行良好的 .cs 文件:

using System;

public class HelloWorld
{
static public void Main ()
{
Console.WriteLine("Enter a number");

int UserNumber = int.Parse(Console.ReadLine());

Console.WriteLine("Your number is: " + UserNumber);
}
}

我在 Xamarin 中打开了这个 Test.cs 文件,它运行正常。然后我选择“运行”>“不调试开始”,显示面板中会弹出以下错误:

Enter a number

Unhandled Exception:
System.ArgumentNullException: Argument 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 /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/number.cs:1084
at System.Number.ParseInt32 (System.String s, NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/number.cs:755
at System.Int32.Parse (System.String s) [0x00000] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/int32.cs:140
at HelloWorld.Main () [0x0000b] in /Users/Yardenbourg/Desktop/Test.cs:9
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentNullException: Argument 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 /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/number.cs:1084
at System.Number.ParseInt32 (System.String s, NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/number.cs:755
at System.Int32.Parse (System.String s) [0x00000] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/int32.cs:140
at HelloWorld.Main () [0x0000b] in /Users/Yardenbourg/Desktop/Test.cs:9
The application was terminated by a signal: SIGHUP

我不确定这里的问题是什么。会不会跟这条线有关?

int UserNumber = int.Parse(Console.ReadLine());

最佳答案

读取堆栈跟踪,它说 Parse 的方法传递了一个 null 参数,但它不能为 null。尝试拆分读取行和解析行,然后确保该行不为 null 或空。

public class HelloWorld
{
static public void Main ()
{
Console.WriteLine("Enter a number");
String input = Console.ReadLine();
int UserNumber = 0;
if(input != null && input != "")
{
UserNumber = int.Parse(input);
}

Console.WriteLine("Your number is: " + UserNumber);
}
}

像这样拆分代码使其更易于阅读和调试。

关于c# - 如何修复 int.Parse 中的 ArgumentNullException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31676535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com