Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

Dominik Huber
Dominik Huber
4,631 Points

My variable gets set to null somehow - can you please help me?

I tried and tried but I can't get it to work and I don't know why. The Error message I got is this: " Bummer! System.ArgumentNullException: Value cannot be null. Parameter name: String. See output for stack trace."

So anyhow the count = int.Parse(Console.ReadLine()); (the one in the if block) gets null assigned, am I right? Why?

The closest I can get to is that code: (Output from Stacktrace at the bottom) /edit: Funny thing. The exact same code in Visual Studio works as intended. Tested it with a positive number, a negative number and zero. All 3 worked just as intended. What am I missing?

using System;

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



            try 
            {
                int loopcount = 0;
                int count = int.Parse(Console.ReadLine());




                while (true) 
                {

                     if (count < 0) 
                     {
                        Console.WriteLine("You must enter a positive number.");
                         count = int.Parse(Console.ReadLine());
                         continue;

                     }

                    if (loopcount < count){
                        Console.WriteLine("Yay!");
                        loopcount += 1;
                    } else {
                        break;
                    }




                }
            }

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




        }
    }
}
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.1/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.1/mcs/class/referencesource/mscorlib/system/number.cs:745 
  at System.Int32.Parse (System.String s) [0x00000] in /builddir/build/BUILD/mono-4.8.1/mcs/class/referencesource/mscorlib/system/int32.cs:120 
  at Treehouse.CodeChallenges.Program.Main () [0x0002d] in <7443b08c0fed4b718a9bdc724518d483>:0 
  at MonoTester.Run () [0x002b3] in MonoTester.cs:169 
  at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:28 

1 Answer

Dominik Huber
Dominik Huber
4,631 Points

Hi I could solve this issue with a int.TryParse() method. Because the problem was if the user enters nothing - so he just clicks enter when the Console.ReadLine() pops up. I handled that with TryParse().

Thank you anyway for your help :)

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,319 Points

Yeah that was my thought, that the user might hit enter without entering anything => then it would throw that exception - but good you fixed it yourself :-)

Happy coding! :-D