0

I need it to pass the MindTap test in order to receive full credit. I got all my methods to pass except GetLists(), and I am getting a weird error that I am having trouble understanding. This is working fine on my console but it looks MindTap has some different test cases that cause it to fail.

GetLists - "This method continuously prompts for talent codes and displays contestants with the corresponding talent until a sentinel value is entered"

        public static void GetLists(int numThisYear, char[] talentCodes, string[] talentCodeStrings, string[] names, char[] talents, int[] counts)
        {
            char input;
            do
            {
                Write("Enter talent to lookup contestants or press 'q' to quit: ");
                input = Char.ToUpper(char.Parse(ReadLine()));
                    for (int i = 0; i < names.Length; i++)
                    {
                        if (input == talents[i])
                        {
                            if (!String.IsNullOrEmpty(names[i]))
                            WriteLine($"{names[i]}");
                        }
                    }
            } while (input != 'q');
        }

Additionally here is the error log from MindTap. Please help.

1) Error : GetListsMethodTest.GetListsTest
System.ArgumentNullException : Value cannot be null.
Parameter name: s
  at System.Char.Parse (System.String s) [0x00003] in <7b90a8780ac4414295b539b19eea7eea>:0 
  at GreenvilleRevenue.GetLists (System.Int32 numThisYear, System.Char[] talentCodes, System.String[] talentCodeStrings, System.String[] names, System.Char[] talents, System.Int32[] counts) [0x0000f] in <4fa6820f6fb84bb29983667e42842788>:0 
  at GetListsMethodTest.GetListsTest () [0x0004d] in <4fa6820f6fb84bb29983667e42842788>:0 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <7b90a8780ac4414295b539b19eea7eea>:0 

And here is the test case

[TestFixture]
public class GetListsMethodTest
{
      const int MIN_CONTESTANTS = 0;
      const int MAX_CONTESTANTS = 30;
      string[] names = new string[MAX_CONTESTANTS];
      char[] talents = new char[MAX_CONTESTANTS];
      char[] talentCodes = {'S', 'D', 'M', 'O'};
      string[] talentCodesStrings = {"Singing", "Dancing", "Musical instrument", "Other"};
      int[] counts = {0, 0, 0, 0};
   [Test]
   public void GetListsTest()
   {
      string consoleInput = "S\nZ";
      using (var inputs = new StringReader(consoleInput))
      {
        using (StringWriter sw = new StringWriter())
        {
          Console.SetIn(inputs);
          Console.SetOut(sw);
          names[0] = "Joe";
          names[1] = "Ann";
          talents[0] = 'S';
          talents[1] = 'M';
          GreenvilleRevenue.GetLists(5, talentCodes, talentCodesStrings, names, talents, counts);  
          StringAssert.Contains("Joe", sw.ToString());
          Assert.IsFalse(sw.ToString().Contains("Ann"), "Ann should not be listed in output for singers");
        }  
      }  

      consoleInput = "M\nZ";
      using (var inputs = new StringReader(consoleInput))
      {
      using (StringWriter sw = new StringWriter())
      {
          Console.SetIn(inputs);
          Console.SetOut(sw);
          names[0] = "Joe";
          names[1] = "Ann";
          talents[0] = 'S';
          talents[1] = 'M';
          GreenvilleRevenue.GetLists(5, talentCodes, talentCodesStrings, names, talents, counts);  
          StringAssert.Contains("Ann", sw.ToString());
          Assert.IsFalse(sw.ToString().Contains("Joe"), "Joe should not be listed in output for musicians");
      }  
      }  
   }
}


rene
  • 41,474
  • 78
  • 114
  • 152
Paul Kim
  • 1
  • 1
  • 2
    `ReadLine()` gives you a null value. Either prevent that or check for null before handing it to the `Parse` method. – rene Oct 14 '20 at 05:37
  • Also, Whenever parsing strings you can't control (provided either by the user or even by a 3rd party software) - prefer `TryParse` over `Parse`. – Zohar Peled Oct 14 '20 at 05:43
  • The exception happens because you are passing `null` to the method. Diagnosis and resolution is the same as for any null value. See duplicate for extensive advice on debugging and fixing exceptions related to null values. – Peter Duniho Oct 14 '20 at 05:52

0 Answers0