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

Eliot Sosa
Eliot Sosa
443 Points

NullReferenceException on code that compiles in Workspaces

Hi there, I am solving the final Task (1 of 3) in the C# basics section. The code here works in workspaces without issue, however does not when submitting for the Task. I am specifically encountering:

System.NullReferenceException: Object reference not set to an instance of an object at Treehouse.CodeChallenges.Program.Main () <0x41864370 + 0x0004b> in :0 at MonoTester.Run () [0x00095] in MonoTester.cs:86 at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:28

What am I missing here? Thanks.

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 = System.Console.ReadLine();

                if (entry.ToLower() == "quit")
                {
                    break;
                }

                try
                {
                    var yays = int.Parse(entry);

                    if (yays <= 0)
                    {
                        Console.WriteLine(yays + " is not an acceptable value.");

                        continue;
                    } else {
                        for (int i = 1; i <= yays; i++)
                        {
                            Console.WriteLine("Yay!");
                        }       
                    }
                }
                catch(FormatException)
                {
                    Console.WriteLine("That is not valid input.");

                    continue;
                }                                  
            }

            Console.WriteLine("Goodbye");
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

Be careful about testing a challenge in an external REPL.

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

Here you have added several features that were not asked for by the challenge. In particular:

  • you have an outer loop that prevents the program from ending after it runs once
  • you require input more than one time
  • you require a special input to cause the program to exit
  • you print an extra message before exiting

Read the instructions carefully for each task and be sure to implement only the features asked for.

Eliot Sosa
Eliot Sosa
443 Points

Thanks Steven. I had assumed that the challenge would be able to see past my "additions", but guess not. I have trimmed the code and passed the challenge.