0

In the past I've had success with adding roles to a user from the users "reaction on a message". However, recently, this code broke and is returning an error:

A ReactionAdded handler has thrown an unhandled exception.:System.NullReferenceException: Object reference not set to an instance of an object.

Now, I've spent a few days trying to figure this out, combing documentation and I honestly am lost as to how to resolve this, especially as I'm still a novice C# coder. guildUser is apparently null but I have no idea how to code it to not be.

I've stripped back many lines of code to simply this. Any help would be awesome.

public async Task GetReactionAddedAsync(Cacheable<IUserMessage, ulong> cachedMessage, ISocketMessageChannel originChannel, SocketReaction reaction)
{
    var guild = _client.GetGuild(177962785009958913);
    var user = reaction.UserId;
    var guildUser = guild.GetUser(user);

    var Members = (guildUser as IGuildUser).Guild.Roles.FirstOrDefault(x => x.Name == ("Members"));
    await guildUser.AddRoleAsync(Members);
}
Peter Csala
  • 4,906
  • 7
  • 11
  • 29
  • Thanks @SvenBardos - I've had many an issue in the past with this, and you are definitely correct. I have made sure role names/references are correct. When I use F11 to debug, Visual Studio trips up at "await guildUser" still leading me to believe its a Null value. – Logan Healy Nov 16 '20 at 10:49
  • Hi and welcome to Stack Overflow. Unfortunately this particular line of question, about NullReferenceException, comes up so often that a canonical question and answer is provided here on the site and I closed yours as a duplicate. Please follow the steps in the answer to try to narrow down why you have a null reference, specifically *what* is null is a good start, and then try to figure out *why* it is null. – Lasse V. Karlsen Nov 16 '20 at 10:54
  • Since you mention `guildUser`, please verify that `guild.GetUser` actually returns a non-null reference, and then since you use `(guildUser as IGuildUser)` a few lines below, make sure the object returned actually implement this interface, otherwise that expression will evaluate to `null`. The nature of this question, however, is that almost always it is not visible in the posted code exactly what is null nor why it is null, otherwise it probably would've leapt at you already, so you need to check that function and the object type being returned. – Lasse V. Karlsen Nov 16 '20 at 10:55
  • Thanks @LasseV.Karlsen for the information. Sorry for the repost. – Logan Healy Nov 16 '20 at 11:18
  • Not at all, the way it's usually done is that a new duplicate is closed, as a duplicate, but usually, as I tried above, some comments are posted as well indicating the things we *can* see that are potential candidates for the problems. Additionally, when you have tracked down the obvious issues, such as identifying exactly which reference is null and also where it came from (or was supposed to come from), if you then have a situation where you cannot explain *why* you get a null reference, edit your question with those details and ask for a reopen. – Lasse V. Karlsen Nov 16 '20 at 11:58
  • However, unfortunately, almost all such questions suffer from the same issue yours does and that is that they lack the details necessary to diagnose the problem without access to the debugger. That's why it is closed as a duplicate against the question with the answer that has the most helpful hints on how to diagnose it. Hopefully this will lead you to getting to the bottom of your problem and find a way to fix it, but if you at the bottom of the well is stuck, edit in some more details and we can take it from there. – Lasse V. Karlsen Nov 16 '20 at 12:00
  • 1
    See my response [here](https://stackoverflow.com/a/64571478/11159372). You need to enable the server member intent in order for guild users to be cached. – Anu6is Nov 16 '20 at 12:01
  • @Anu6is Thankyou for the information. This was the issue. As I suspected initially but didn't mention, I was fairly sure there was no issue with code. Discord at some point has added or reset option within the developer portal. I navigated there to find the checkboxes and toggles which now once again, allows the proper use of parameters from the User Events (GetAddedReactions, GetRemovedReactions etc). Thankyou all for your help. – Logan Healy Nov 18 '20 at 07:44

0 Answers0