C# 为什么它给我一个NullReferenceException却没有错误

C# 为什么它给我一个NullReferenceException却没有错误,c#,substring,nullreferenceexception,C#,Substring,Nullreferenceexception,创建一个程序用户可以输入单词并弹出首字母缩略词,这很简单,但我必须使用字典,键必须使首字母缩略词为空。我得到了NullReferenceException。我不明白为什么看不到空 仅使用visual studio 2015,我已经尝试了太多,我的头脑都崩溃了,甚至无法很好地描述问题。抱歉 private string fullSentence; private string[] words = new string[5]; private Dictionary<ch

创建一个程序用户可以输入单词并弹出首字母缩略词,这很简单,但我必须使用字典,键必须使首字母缩略词为空。我得到了NullReferenceException。我不明白为什么看不到空

仅使用visual studio 2015,我已经尝试了太多,我的头脑都崩溃了,甚至无法很好地描述问题。抱歉

    private string fullSentence;
    private string[] words = new string[5];
    private Dictionary<char, string> acronymDictionary = new Dictionary<char, string>();
    public Acronym(string sentence)
    {
        fullSentence = sentence;
        string[] words = sentence.Split(' ');

    }

    public void BuildAcronym()
    {

        char[] key = new char[words.Length];
        //key = null;
        //foreach (string info in words)
        //{
            for (int i = 0; i < words.Length; i++)
            {
                key[i] = Convert.ToChar(words[i].Substring(0,1)); //this is where the problem is
            }
私有字符串完整语句;
私有字符串[]字=新字符串[5];
私有词典首字母缩略词Dictionary=新词典();
公共首字母缩略词(字符串句)
{
完整句子=句子;
字符串[]单词=句子。拆分(“”);
}
公共建筑首字母缩写()
{
char[]key=新字符[words.Length];
//key=null;
//foreach(文字中的字符串信息)
//{
for(int i=0;i

我希望它能运行,特别是关键字字符[]将由单词[]中每个单词的第一个字母填充,因此我可以将关键字[]设置为字典的关键字

单词[I]
可以为空

您可以在抛出的行上放置断点,并在“监视”窗口中检查
单词[i]
的值。请尝试此操作

    private string fullSentence;
    private string[] words;
    private Dictionary<char, string> acronymDictionary = new Dictionary<char, string>();
    public Acronym(string sentence)
    {
        fullSentence = sentence;
        words = sentence.Split(' ');

    }

    public void BuildAcronym()
    {
        if(words != null)
        { 
        char[] key = new char[words.Length];
        //key = null;
        //foreach (string info in words)
        //{
            for (int i = 0; i < words.Length; i++)
            {
                key[i] = Convert.ToChar(words[i].Substring(0,1)); //this is where the problem is
            }
         }
     }
私有字符串完整语句;
私有字符串[]字;
私有词典首字母缩略词Dictionary=新词典();
公共首字母缩略词(字符串句)
{
完整句子=句子;
单词=句子。拆分(“”);
}
公共建筑首字母缩写()
{
如果(字!=null)
{ 
char[]key=新字符[words.Length];
//key=null;
//foreach(文字中的字符串信息)
//{
for(int i=0;i
您正在使用构造函数中具有相同名称的局部变量重写
words
成员。
words=句子。拆分(“”);
哪一行代码引发异常?可能是