I am developing a C# program in .NET 8
I have a generic host with a bunch of services for a discord bot. In one I have code like this (simplified code)
public MessageService(DiscordSocketClient client, ILogger<DiscordClientService> logger)
: base(client, logger)
{
client.MessageUpdated += OnMessageUpdated;
}
private Task OnMessageUpdated(Cacheable<IMessage, ulong> oldMessage, SocketMessage newMessage, ISocketMessageChannel channel)
{
Task.Run(async () =>
{
if (newMessage is not SocketUserMessage message) return;
var context = new SocketCommandContext(Client, message);
if (context.Channel.Id == _botOptions.ChannelId)
await CheckChannelAsync(context); // <--- Error here
}
return Task.CompletedTask;
}
private async Task<bool> CheckChannelAsync(SocketCommandContext context)
{
if (context.Message.Attachments.Count == 0)
{
string[] allowedSites = File.ReadAllLines(Utilities.GetLocalFilePath("AllowedSites.txt"));
if (!Array.Exists(allowedSites, context.Message.Content.Contains))
{
string errorMessage = $"Your message doesn't contain any allowed website";
await MessageDeleterHandlerAsync(context, errorMessage);
return false;
}
}
else
{
var attachment = context.Message.Attachments.First();
if (attachment.Width < 40 || attachment.Height < 40)
{
string errorMessage = $"The picture is too small!!";
await MessageDeleterHandlerAsync(context, errorMessage);
return false;
}
}
return true;
}
sometimes I get a burst of the same error triggering multiple times per minute and then it goes away for some days
System.ArgumentException: Delegate to an instance method cannot have null 'this'.
at System.MulticastDelegate.ThrowNullThisInDelegateToInstance()
at MyBot.Services.MessageService.CheckChannelAsync(SocketCommandContext context)
at MyBot.Services.MessageService.<>c__DisplayClass15_0.<<OnMessageUpdated>b__0>d.MoveNext() in /my/bot/Services/MessageService.cs:line 101
I don't know what the error message means. I'm not using delegates as far as I know so I don't know what to check to find out
I have tried logging if context is null but unsurprisingly it isn't as I dereference it fine before the error happens
This is using Discord.NET
EDIT: added the logic for CheckChannelAsync, also I'm not creating a delegate in the same way as the duplicate question so that one doesn't answer my question
This problem occurs when create a delegate directly from a method-group, i.e.
and it turns out that
someObjisnull; this isn't aNullReferenceException, but it is basically comparable. So: look for a method-group conversion (to a delegate), which could be an event subscription, and check that the target object is notnull.I can't see any of these in your code - the only candidate is:
which is implicitly
but we can we reasonably sure that
thisis notnull; I'm guessing the real cause is in the code not shown. Note that the cause could be hidden in things like LINQ, for example:if this is
IEnumerable<T>,Clauseis a method-group (most likelybool Clause(T value)), andfilterturns out to benull.