-2

I'm trying to run a program I wrote in MS Visual Studio (mac)enter image description here, and when I start the program without debugging, the terminal won't come up and the program won't start. An internal terminal pops up inside VS with the message:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Cohen_Brett_HW1.Program.CheckCode(String strInput) in /Users/brettcohen/Desktop/MIS 333K/Cohen_Brett_HW1_WORKING/Cohen_Brett_HW1_WORKING/Program.cs:line 122 at Cohen_Brett_HW1.Program.Main(String[] args) in /Users/brettcohen/Desktop/MIS 333K/Cohen_Brett_HW1_WORKING/Cohen_Brett_HW1_WORKING/Program.cs:line 41

My code looks like this:

using System;

namespace Cohen_Brett_HW1
{
    class Program
    {
        //Named constants
        public const decimal TacoPrice = 2.00m;
        public const decimal SandwichPrice = 7.00m;
        public const decimal TaxRate = .0825m;


        static void Main(string[] args)
        {
            //Input variables
            String strCustCodeInput;
            int intTacoCountInput;
            String strTacoCountInput;
            int intSandwichCountInput;
            String strSandwichCountInput;


            //Variable for customer code validity
            Boolean boolCodeValid;


            //Get the customer code
            do
            {
                //Request input
                Console.WriteLine("Please enter the customer code:");
                strCustCodeInput = Console.ReadLine();

                //Make sure the code is valid
                boolCodeValid = CheckCode(strCustCodeInput);

            } while (boolCodeValid == false); //loop if customer code is invalid


            do
            {
                //Get the taco order count
                do
                {
                    //Request user input
                    Console.WriteLine("Enter the number of tacos you would like to order:");

                    //Read the input
                    strTacoCountInput = Console.ReadLine();

                    //Pass the string to CheckItem method and assign the result to intTacoCountInput
                    intTacoCountInput = CheckItem(strTacoCountInput);
                } while (intTacoCountInput == -1); //Loop if they enter an invalid number


                //Get the sandwich order count
                do
                {   //Request user input
                    Console.WriteLine("Enter the number of sandwiches you would like to order:");

                    //Read the input
                    strSandwichCountInput = Console.ReadLine();

                    //Pass the string to CheckItem method and assign the result to intTacoCountInput
                    intSandwichCountInput = CheckItem(strSandwichCountInput);

                } while (intSandwichCountInput == -1); //Loop if they enter an invalid number

                if (intTacoCountInput + intSandwichCountInput == 0)
                {
                    Console.WriteLine("Total number of items ordered must be greater than 0." + "\n");

                }
            } while (intTacoCountInput + intSandwichCountInput < 1); //make sure that total items ordered is at least one


            //Output variables

            //Total item count
            int intTotalItemCount = intTacoCountInput + intSandwichCountInput;

            //Taco subtotal
            decimal TacoSubtotal = TacoPrice * intTacoCountInput;

            //Sandwich subtotal
            decimal SandwichSubtotal = SandwichPrice * intSandwichCountInput;

            //Combined Subtotal
            decimal CombinedSubtotal = TacoSubtotal + SandwichSubtotal;

            //Tax Amount
            decimal TaxAmount = CombinedSubtotal * TaxRate;

            //Grand Total
            decimal GrandTotal = CombinedSubtotal + TaxAmount;

            //Console output
            Console.WriteLine("Customer Code: " + strCustCodeInput.ToUpper());
            Console.WriteLine("Total Items: " + intTotalItemCount);
            Console.WriteLine("Taco Subtotal: " + (TacoSubtotal.ToString("C")));
            Console.WriteLine("Sandwich Subtotal: " + (SandwichSubtotal.ToString("C")));
            Console.WriteLine("Full Subtotal: " + (CombinedSubtotal.ToString("C")));
            Console.WriteLine("Sales Tax: " + (TaxAmount.ToString("C")));
            Console.WriteLine("Grand Total: " + (GrandTotal.ToString("C")));

            //Keep console open
            Console.WriteLine("Press any key to exit");
            Console.ReadLine();

        }


        //This method validates the customer code
        public static Boolean CheckCode(String strInput)
        {
            if (strInput.Length < 4 || strInput.Length > 6)
            {
                Console.Write("Please enter a valid code, 4 to 6 characters, letters only." + "\n");
                return false;
            }

            else
            {   //make sure that input is letters only
                foreach (char c in strInput)
                {
                    if (!char.IsLetter(c))
                    {
                        Console.WriteLine("Customer code must consist of letters only." + "\n");
                        return false;
                    }

                    else
                    {
                        return true;
                    }
                }
                return true;
            }
        }


        //This method validates the customer selection
        static int CheckItem(String strInput)
        {
            //Establish integer variable
            int NowInt;

            //Convert the strInput to an integer
            NowInt = Convert.ToInt32(strInput);

            //Validate
            if (NowInt > -1)
            {
                return NowInt;
            }
            else
            {
                Console.Write("Please enter a valid number greater than or equal to 0." + "\n");
                return -1;
            }
        }
    }
}

Thanks in advance for your help!

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
Brett Cohen
  • 29
  • 2
  • 4
  • What is line 41 in the code? – Chetan Sep 18 '18 at 04:56
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Tetsuya Yamamoto Sep 18 '18 at 04:57
  • It looks like you pass `null` to your method `CheckCode`. You might want to check for null there. – Blacktempel Sep 18 '18 at 04:57
  • @ChetanRanpariya //Make sure the code is valid boolCodeValid = CheckCode(strCustCodeInput); – Brett Cohen Sep 18 '18 at 04:57
  • Did you debug the code to check why NullReferenceException is thrown? The error also says that line 122 in CheckCode method is throwing error. You need to check there too. – Chetan Sep 18 '18 at 04:59

1 Answers1

0

Using a .NET Fiddle I was able to run your program just by placing public before your method static void Main(string[] args) and before class Program. I also changed your CheckCode method because there wasn't a proper validation to check if there would only be letters.

Follow the link provided to check the full code otherwise just add public where I told you, add this library using System.Text.RegularExpressions; and modify your CheckCode method to match this:

public static Boolean CheckCode(String strInput)
{
    if (strInput.Length < 4 || strInput.Length > 6)
    {
        Console.Write("Please enter a valid code, 4 to 6 characters, letters only." + "\n");
        return false;
    }
    else
    {   //make sure that input is letters only
        return Regex.IsMatch(strInput, @"^[a-zA-Z]+$");
    }
}
Pietro Nadalini
  • 1,722
  • 3
  • 13
  • 32