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

NullReferenceException issue

UPDATE Thanks to Steven Parker and previous posts, figured it out. Too complicated for the task. Here's the link if somebody's stuck by the some issues: https://teamtreehouse.com/community/final-2

Spent some time figuring out how to solve the Challenge, but got stuck. After checking, all time appears:

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

and the thing is, in Workspaces my code executes just fine! Everything works. Can somebody, please, at least address me where to search for the error. Thank you. Original code below:

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();
                if (entry.ToLower() == "quit")
                {
                    break;
                }

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

                    for (int x = 1; x <= count; x++)
                    {
                        Console.WriteLine("Yay!");
                    }

                }

                catch(FormatException)
                {
                  Console.WriteLine("That is not valid input");
                  continue;
                }
                catch(NullReferenceException)
                {
                }
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,261 Points

You probably don't want to catch NullReferenceException, perhaps ever.

It's a sure sign of something seriously wrong in a program.

What happens in the workspace if you don't catch it?

And if it only happens in the challenge, just ignore it. We already know that the challenge evaluates more than just whether the program runs. And you know how to fix the challenge from the previous post.

Everything's now is OK. I meant, my code was too complicated for the task, therefore I removed all redundant and it successfully evaluated.

Thank you.

Steven Parker
Steven Parker
231,261 Points

Yes, the previous answer addressed the issues with passing the challenge.

But it seemed worthwhile to cover that risky use of catch shown here.

Happy coding!  —sp:sparkles: