C# e、 After.VoiceChannel.Name Discord.NET-Can';我们不能找出空错误

C# e、 After.VoiceChannel.Name Discord.NET-Can';我们不能找出空错误,c#,nullreferenceexception,discord,discord.net,C#,Nullreferenceexception,Discord,Discord.net,在您判断我发布另一个NullReferenceException问题之前,请阅读我的代码。我在谷歌上搜索NullReferenceException错误已经有一段时间了,但它并没有解释为什么这个特定部分会给我一个错误 if (e.After.VoiceChannel.Name != null) 我还查看了Discord.NET的文档,但是也没有任何结果。这是全部文件 using Discord; using Discord.Commands; using System; using

在您判断我发布另一个NullReferenceException问题之前,请阅读我的代码。我在谷歌上搜索NullReferenceException错误已经有一段时间了,但它并没有解释为什么这个特定部分会给我一个错误

    if (e.After.VoiceChannel.Name != null)
我还查看了Discord.NET的文档,但是也没有任何结果。这是全部文件

using Discord;
using Discord.Commands;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiscBot
{
    class myBot
    {
        DiscordClient discord;

        public myBot()
        {

            discord = new DiscordClient(x => 
            {
                x.LogLevel = LogSeverity.Info;
                x.LogHandler = Log;
            });

            discord.UsingCommands(x => 
            {
                x.PrefixChar = '~';
                x.AllowMentionPrefix = true;
                x.HelpMode = HelpMode.Public;
            });

            var commands = discord.GetService<CommandService>();

            commands.CreateCommand("greet")
                .Description("Does a thing")
                .Parameter("theGreeted", ParameterType.Unparsed)
                .Do(async (e)=> 
                {
                    var msgtoRead = e.Channel.DownloadMessages(1);

                    await e.Channel.SendTTSMessage("Hello " + e.GetArg("theGreeted"));
                });

            discord.UserUpdated += async (s, e) => {
                var channel = e.Server.FindChannels("general", ChannelType.Text).FirstOrDefault();
               string usrnm = e.After.Name;

                // This shouldn't get an error
                // But when I run the code I
                // get a Null Reference Exception

                if (e.After.VoiceChannel.Name != null)
                {
                    await channel.SendMessage(usrnm + " to " + e.After.VoiceChannel.Name);
                }
                else
                {

                    await channel.SendMessage(usrnm + " exited");
                }
            };


            discord.UserJoined += async (s, e) =>
            {
                var channel = e.Server.FindChannels("mainbois", ChannelType.Text).FirstOrDefault();
                var user = e.User;
                await channel.SendTTSMessage(string.Format("Another human has entered..."));
            };

            discord.ExecuteAndWait(async () =>
            {
                await discord.Connect("MYBOTTOKEN", TokenType.Bot);
            });
        }

        private void Log(object sender, LogMessageEventArgs e)
        {
        Console.WriteLine(e.Message);
        }
    }
}     
使用Discord;
使用不协调命令;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间DiscBot
{
类myBot
{
不和客户不和;
公共myBot()
{
discord=新的DiscordClient(x=>
{
x、 LogLevel=LogSeverity.Info;
x、 LogHandler=Log;
});
不协调。使用命令(x=>
{
x、 PrefixChar='~';
x、 AllowMationPrefix=true;
x、 HelpMode=HelpMode.Public;
});
var commands=discord.GetService();
commands.CreateCommand(“问候”)
.Description(“做某事”)
.Parameter(“thehelleed”,ParameterType.Unparsed)
.Do(异步(e)=>
{
var msgtoRead=e.Channel.DownloadMessages(1);
等待e.Channel.sendtsmessage(“Hello”+e.GetArg(“thehelleed”));
});
discord.UserUpdated+=异步(s,e)=>{
var channel=e.Server.FindChannels(“常规”,ChannelType.Text).FirstOrDefault();
字符串usrnm=e.After.Name;
//这不应该出现错误
//但是当我运行代码时
//获取空引用异常
if(e.After.VoiceChannel.Name!=null)
{
等待channel.SendMessage(usrnm+“到”+e.After.VoiceChannel.Name);
}
其他的
{
等待通道发送消息(usrnm+“退出”);
}
};
discord.UserJoined+=async(s,e)=>
{
var channel=e.Server.findcchannels(“mainbois”,ChannelType.Text).FirstOrDefault();
var user=e.user;
wait channel.sendtsmessage(string.Format(“另一个人已输入…”));
};
discord.ExecuteAndWait(异步()=>
{
等待discord.Connect(“MYBOTTOKEN”,TokenType.Bot);
});
}
私有无效日志(对象发送者,LogMessageEventArgs e)
{
控制台写入线(e.Message);
}
}
}     
非常感谢您的帮助。提前谢谢


另外,如果我在某个地方犯了一个简单的错误,请惩罚我P

发现我无法检查

if (e.After.VoiceChannel.Name != null)
如果VoiceChannel为空。换句话说,我不能检查基于null的值(如果有意义的话)。我的新代码看起来像

if (e.After.VoiceChannel != null)

感谢所有花时间查看我的代码的人:D

编辑:
e.After.VoiceChannel
为空。我对这件事的了解还不足以说明原因。