Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perfect Final

Deborah Reyes
Deborah Reyes
563 Points

System.ArgumentNullException

I keep getting this exception when I try to run my code:

System.ArgumentNullException: Value cannot be null. Parameter name: String at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00054] in /builddir/build/BUILD/mono-4.8.0/mcs/class/referencesource/mscorlib/system/number.cs:1074 at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /builddir/build/BUILD/mono-4.8.0/mcs/class/referencesource/mscorlib/system/number.cs:745 at System.Int32.Parse (System.String s) [0x00000] in /builddir/build/BUILD/mono-4.8.0/mcs/class/referencesource/mscorlib/system/int32.cs:120 at Treehouse.CodeChallenges.Program.Main () [0x00010] in <3ecdb2bf57d24db7832c050d1b856030>:0 at MonoTester.Run () [0x00095] in MonoTester.cs:86 at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:28

I'm honestly just confused...

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main ()
        {
            while (true)
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                var entry = Console.ReadLine();

                try
                {
                    var numTimes = int.Parse(entry);
                    if (numTimes <= 0)
                    {
                        Console.WriteLine("You must enter a positive number.");
                        continue;
                    }
                    while (numTimes > 0)
                    {
                        Console.WriteLine("Yay!");
                        numTimes--;
                    }
                }

                catch (FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                }
            }
        }
    }
}

1 Answer

Jon Wood
Jon Wood
9,884 Points

What input are you putting in when you run it? I'm thinking there may not be any input and when it tries to parse it it gives that error.