How do I fix this ArgumentNullException in int.Parse?

爷,独闯天下 提交于 2019-12-02 04:32:18

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!