Como corrijo esse ArgumentNullException no int.Parse?
Este é o arquivo .cs roda bem no 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);
}
}
Abri este arquivo Test.cs no Xamarin, que funcionou corretamente. Então eu escolho 'Executar'> 'Iniciar sem depuração' e esses erros aparecem no painel da tela:
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
Não tenho certeza de qual é o problema aqui. Poderia ter a ver com essa linha?
int UserNumber = int.Parse(Console.ReadLine());