discordnullpointerexceptiondiscord-jda

JDA getGuildById() returning null


I am attempting to construct my first java bot, and while attempting to set up a slash command exactly as the tutorial I'm following says to, I've been running into this issue where the return value of "net.dv8tion.jda.api.JDA.getGuildById(String)" is null. I am using the server ID of the server I'm testing the bot in and {myId} is the placeholder. {myToken} is a placeholder as well. Please help! 😭

import javax.security.auth.login.LoginException;

import commands.BotCommands;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;

public class DiscordBot {
    
    public static void main(String[] args) throws LoginException, InterruptedException {
        JDA jda = JDABuilder.createDefault("{myToken}") 
                .setActivity(Activity.watching("you sleep"))
                .addEventListeners(new BotCommands())
                .build();
        
        if(jda.getGuildById("{myId}") == null) {
            System.out.println("null");
        }
        
        jda.getGuildById("{myId}").upsertCommand("schedule", "Schedule a time to kick a user from a voice channel").queue();
        
        
        
    }
}

I used that if statement to check if getGuildById() was returning null, and it is.


Solution

  • As the answer states all you really gotta do is call awaitReady()
    to block the main thread until JDA is ready.

    Here's the documentation for it if you're curious documentation