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

James Hoffman
James Hoffman
6,354 Points

Treehouse gives me a System.ArgumentNullException: value cannot be null. Parameter name: String.

Tried adding:

catch(ArgumentNullException) { Console.WriteLine("Null!); }

but it didn't work.

Program.cs
using System;

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

                try
                {
                    var iEntry = int.Parse(sEntry);

                    if (iEntry > 0)
                    {
                        while (iEntry > 0)
                        {
                            Console.WriteLine("Yay!");
                            iEntry -= 1;
                        }
                    } 
                    else
                    {
                        Console.WriteLine("Negative number!");
                    }   
                }
                catch (FormatException)
                {
                    Console.WriteLine("Not a valid input!");
                    continue;
                }
            }
        }
    }
}

1 Answer

Jon Wood
Jon Wood
9,884 Points

What input are you giving it? I ran it a few times and didn't get an exception.

Also, the while(true) that surrounds everything will always run your application unless the process is terminated. That may be able to be removed.