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

ryan plunkett
ryan plunkett
3,500 Points

c# basics final step 2

Howdy I can run this in workspaces and its fully functional however it doesn't pass the Code challenge any help would be appreciated.

this is the error System.NullReferenceException: Object reference not set to an instance of an object at Treehouse.CodeChallenges.Program.Main () [0x00010] in :0 at MonoTester.Run () [0x000b5] in MonoTester.cs:91 at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:28

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

                if(cbrate.ToLower() == "quit")
                {
                    break;
                }
                try
                {   
                    var counter = 0;
                    var selcon = int.Parse(cbrate);
                    if(selcon <= 0)
                    {
                        Console.WriteLine("You must enter a whole number.");

                    }

                    while (counter < selcon)
                    {
                    Console.WriteLine("Yay : ");
                    counter++;

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

                }
            }
        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,235 Points

You have an outer loop that prevents the program from ending.

A program built according to the instructions will prompt and accept input one time, and based on the input it will either print "Yay!"s or an error mesage. Either way, it should end. With the extra loop it only ends if the user enters "quit", which is not the behavior asked for in the instructions.

Also, you're issuing the same error message for a negative number as you do for a format exception. But the challenge is expecting a different specific message for a negative input. Check the instructions again.

ryan plunkett
ryan plunkett
3,500 Points

Thank you, simplified it works.

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { try { Console.Write("Enter the number of times to print \"Yay!\": "); var cbrate = Console.ReadLine(); var selcon = int.Parse(cbrate); var counter = 0;

            while (counter < selcon)
            {
            Console.Write("Yay : ");
            counter++;
            }
        }
        catch(FormatException)
        {
            Console.WriteLine("You must enter a whole number.");
        }

    }
}
Steven Parker
Steven Parker
231,235 Points

You didn't need a separate test and message for a negative input?

Also, when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.