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

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

MonoTester error message although program works fine

Hi, I'm trying to complete the final code challenge of the C# Basics course. The task requires the program to print "Yay!" as many times as the number that is entered and to catch and pass Format Exceptions when the user enters something else than an integer.

My code works in Visual Studio and in my Workspace environment just as required in the task instructions, but when I press "Check work" after pasting the same code in the editor window, I get the following error message:

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 () [0x00012] in <32a2be91c59b40c49ea6b902dd449240>:0 at MonoTester.Run () [0x00095] in MonoTester.cs:86 at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:28

I suspect that I may get this error message because I have two loops in my code instead of just one, but that's only what I'm guessing.

BR, Kimmo

PS. I posted an earlier message related to the same task and thanks to the replies I got, I was able to change the code a little bit (I added another loop), so that it know works perfectly in Visual Studio and in my Workspace environment. Thanks for the comments!

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {

            Console.Write("Enter the number of times to print \"Yay!\": ");

            while (true)
            {
                int counter = 0;
                string entry = Console.ReadLine();

                try
                {
                    while (true)
                    {
                        int number = int.Parse(entry);
                        counter = counter + 1;

                        if (counter > number)
                        {
                            break;
                        }
                        Console.WriteLine("Yay!");

                    }
                }

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

        }
    }
}

1 Answer

Steven Parker
Steven Parker
229,786 Points

:warning: Be careful about testing a challenge in an external REPL or IDE.

If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.

In this case, you have added an extra loop that prevents the program from ending if bad values are entered..

A program that follows the challenge instructions will only ask for input one time, and either print "Yay!"s or an error message. But either way it should end.

Also, if I remember correctly, shouldn't there be a separate error message if a negative value is entered? There doesn't seem to be code to do that here.

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

Thanks! It made all the difference to me that you pointed out that the program is supposed to end. I changed my code and made the program to end after its output. I also added the error message for the negative integer entries.