我收到此错误当我尝试运行该程序时,System.Core.dll中出现未处理的“System.ArgumentNullException”类型的异常异常。它给了我一些建议,例如“在调用方法之前确定对象为null”和“使用”new“关键字来创建对象实例”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
protected static string userString { get; set; }
public virtual void GetUserString()
{
Console.WriteLine("Enter a string of letters: ");
userString = Convert.ToString(Console.ReadLine());
}
class ProgramOrder : Program
{
public virtual void SetOrder()
{
bool moveOn = userString.All(Char.IsLetter); //Error here
base.GetUserString();
if (moveOn == true)
{
char[] array = userString.ToCharArray();
for (int i = array.Length - 1; i >= 0; i--)
{
Console.Write("{0}", array[i]);
}
}
else
{
Console.WriteLine("You used characters that are not letters.");
Console.WriteLine("Please use ONLY letters.");
SetOrder();
}
}
}
class TestProgram
{
static void Main(string[] args)
{
ProgramOrder P1 = new ProgramOrder();
P1.SetOrder();
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
}
}
}
答案 0 :(得分:1)
首先致电base.GetUserString
然后检查你的信件:
base.GetUserString();
bool moveOn = userString.All(Char.IsLetter);
答案 1 :(得分:0)
您尝试访问它时,您的Userstring为空。
您可以先检查null,或者指定一个类似string.empty的默认值。