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

How do I fix this ArgumentNullException in int.Parse?(如何在 int.Parse 中修复此 ArgumentNullException?)
本文介绍了如何在 int.Parse 中修复此 ArgumentNullException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is the .cs file runs fine in 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' 并且这些错误会在显示面板中弹出:

I opened this Test.cs file in Xamarin, which worked properly. Then I choose 'Run' > 'Start Without Debugging' and these errors pop up in the display panel:

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

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

I am not sure what the problem here is. Could it be to do with this line?

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

推荐答案

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

Read the stack trace, it says the method of Parse was passed a parameter of null, but it cannot be null. Try splitting the read line and the parsing, and then making sure the line is not null or empty.

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

这样拆分代码更容易阅读和调试.

Splitting up code like this makes it easier to read and easier to debug.

这篇关于如何在 int.Parse 中修复此 ArgumentNullException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)