C#:未将对象引用设置到对象的实例 (System.NullReferenceException)

图片说明
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleExamples
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main(string[] args)
{
Console.Write("请输入x和y(例如12,15),然后按回车键:");
string s = Console.ReadLine();
string[] a = s.Split(',');
int x = int.Parse(a[0]);
int y = int.Parse(a[1]);
int z = x * y;
Console.WriteLine("x*y={0}", z);
Console.WriteLine("请按任意键结束程序。");
Console.ReadKey();
}
}
}

不是很明显。。。s为null,应该是直接回车了。而且你看后续代码要转换,你应该判断下s变量的有效性。。要不乱输入还是一样报错

         string s=Console.ReadLine();

        if (string.IsNullOrEmpty(s) || !Regex.IsMatch(@"^\d+,\d+$", s))
        {
            Console.WriteLine("输入有误!");
            return;
        }

先判断一下:s != null,再调用Split

应该是字符串s没有赋值,也就是没有输入内容。