如何在int.Parse中修复此ArgumentNullException?

时间:2015-07-28 12:36:55

标签: c# parsing xamarin mono argumentnullexception

这是.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' > '在没有调试的情况下启动'并在显示面板中弹出这些错误:

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());

2 个答案:

答案 0 :(得分:1)

读取堆栈跟踪,它说Parse的方法传递的参数为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);
    }
}

分割这样的代码使其更易于阅读和更容易调试。

答案 1 :(得分:0)

I bet you're using a C# -> Mac -> Xamarin.Mac project. By default, these programs don't use an interactive console, which you're trying to use when you call Console.ReadLine().

Try creating a new solution; pick C# -> Console Project instead to have the interactive console working.