Search code examples
javanullshardingdiscord-jda

builder.build().getShardManager() returns null (JDA Discord Bot) | how do I call the shardmanager?


I am new to JDA discord bot and encountered a problem with the builder.build().getShardManager() method. At some points in my code I need the shardManager but I dont know how to get it. I found out that getShardManager() can be called on an JDA object (here: JDABuilder.build()) but it only returns a null reference. (The main class crashes at line 4 with a nullpointerexception) (without the shardmanager the bot works)

How do I obtain the shardmanager correctly?

The main code

this.builder = JDABuilder.createDefault(TOKEN);
builder.addEventListeners(new CommandHandler());
shardManager = this.builder.build().getShardManager();
shardManager.setActivity(Activity.playing("Do smth."));

Solution

  • You should use the JDA Object and not the ShardManager.

    try something like this:

    this.builder = JDABuilder.createDefault(TOKEN);
    builder.addEventListeners(new CommandHandler());
    JDA jda = this.builder.build();
    jda.getPresence().setActivity(Activity.playing("Hello World!"));
    

    You can also set the Activity before building the Bot

    this.builder = JDABuilder.createDefault(TOKEN);
    builder.addEventListeners(new CommandHandler());
    builder.setActivity(Activity.playing("Hello World!"));
    JDA jda = this.builder.build();