I'm trying to run a program I wrote in MS Visual Studio (mac), 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!