我如何修复这个ArgumentNullException在int.Parse
本文关键字:int Parse ArgumentNullException 何修复 | 更新日期: 2023-09-27 18:05:19
这是。cs文件在Mono中运行良好:
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文件,它正常工作。然后我选择'Run'> 'Start Without Debugging',这些错误弹出在显示面板:
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# -> Mac -> Xamarin.Mac
项目。默认情况下,这些程序不使用交互式控制台,当您调用Console.ReadLine()
时,您将尝试使用交互式控制台。
尝试创造一个新的解决方案;选择C# -> Console Project
以使交互式控制台工作