C# VS Studio终端无法启动,程序无法启动
我正在尝试运行我在MS Visual Studio mac中编写的程序,当我在没有调试的情况下启动程序时,终端将无法启动,程序也无法启动。VS内部会弹出一个内部终端,并显示以下消息: 未处理的异常:System.NullReferenceException:对象引用 未设置为对象的实例。在 Cohen_Brett_HW1.Program.CheckCodeString输入 /用户/brettcohen/Desktop/MIS 333K/Cohen_Brett_HW1_WORKING/Cohen_Brett_HW1_WORKING/Program.cs:line 122在Cohen_Brett_HW1.Program.MainString[]参数中 /用户/brettcohen/Desktop/MIS 333K/Cohen_Brett_HW1_WORKING/Cohen_Brett_HW1_WORKING/Program.cs:line 41 我的代码如下所示:C# VS Studio终端无法启动,程序无法启动,c#,terminal,C#,Terminal,我正在尝试运行我在MS Visual Studio mac中编写的程序,当我在没有调试的情况下启动程序时,终端将无法启动,程序也无法启动。VS内部会弹出一个内部终端,并显示以下消息: 未处理的异常:System.NullReferenceException:对象引用 未设置为对象的实例。在 Cohen_Brett_HW1.Program.CheckCodeString输入 /用户/brettcohen/Desktop/MIS 333K/Cohen_Brett_HW1_WORKING/Cohen_
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;
}
}
}
}
提前感谢您的帮助 使用一个函数,我只需将public放在方法之前,并在[]args和类程序之前,就可以运行您的程序。我还更改了CheckCode方法,因为没有适当的验证来检查是否只有字母
按照提供的链接检查完整代码,否则只需在我告诉您的地方添加public,使用System.Text.RegularExpressions添加此库;并修改您的CheckCode方法以匹配以下内容:
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]+$");
}
}
代码中的第41行是什么?可能的重复看起来像是将null传递给方法CheckCode。您可能需要在此处检查null。@ChetanRanpariya//确保代码有效boolCodeValid=CheckCodestrCustCodeInput;您是否调试了代码以检查引发NullReferenceException的原因?该错误还表示CheckCode方法中的第122行正在抛出错误。你也需要去那里看看。