CreaM
CreaM

Reputation: 36

Why is the JDA returning a NullPointerException?

I'm currently trying to use the getMemberById() method, but it always returns a null, even when I give it specific instructions or a specific ID that I know is correctly formatted.

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        Member member = event.getGuild().getMemberById("500364917736734734");
}

I know this ID is valid because when I use a boolean to detect if a user's ID is equal to this specific ID, it returns as non-null. It gives me true when it is.

Your help would be very much appreciated!

Upvotes: 0

Views: 1400

Answers (2)

Minn
Minn

Reputation: 6131

You can use retrieveMemberById instead:

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
    event.getGuild().retrieveMemberById("500364917736734734").queue(member -> {
        ...
    });
}

This is necessary because the member might not be cached when you call getMemberById. By default, JDA will not load all members into cache on startup. For this purpose, a number of methods are available to load members dynamically. This is further explained in the JDA Wiki.

Upvotes: 1

Pieter12345
Pieter12345

Reputation: 1789

It is very well possible that you are calling getMemberById(String) while the ID should be entered as a long into getMemberById(long) JavaDocs. Considering that you might want to get the Member for which this event fires, you should use GuildMessageReceivedEvent.getMember() if that's the case.

Upvotes: 0

Related Questions