Discord Bot

flozge

Neues Mitglied
Hallo ich programmiere zur Zeit einen Discord Bot mit Java Maven. Allerdings habe ich ein kleines Problem das letzte jda might be null, aber in dem Tutorial das ich angeschaut habe, wurde es benutzt was muss ich um schreiben?
Java:
package de.flozge.discord.bot.core;

import de.flozge.discord.bot.commands.Broadcast;
import de.flozge.discord.bot.listeners.InfoListener;
import de.flozge.discord.bot.util.Const;
import de.johnnyjayjay.discord.api.command.CommandSettings;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.entities.Game;

import javax.security.auth.login.LoginException;

public class Main {

    public static void main(String[] args) {

        JDABuilder builder = new JDABuilder(AccountType.BOT);
        builder.setToken(Const.TOKEN)
                .setStatus(OnlineStatus.ONLINE)
                .addEventListener(new InfoListener())
                .setGame(Game.of(Game.GameType.DEFAULT, "Ich bin flozge's Bot"));

        builder.setAutoReconnect(true);




        JDA jda = null;
        try {
             jda = builder.buildBlocking();
        } catch (LoginException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new CommandSettings("f!,", jda)
                .add("believe", new Broadcast())
                .activate();

    }
}
 

mrBrown

Super-Moderator
Mitarbeiter
Das Problem ist aber höchstens eine IDE-Warung, oder?

angenommen, im try fliegt eine Exception, dann ist jda nach dem try-catch immer noch null.

Eine mögliche Lösung wäre z.B, den Teil nach dem catch mit in das try zu schieben.
 

flozge

Neues Mitglied
Hallo danke für die Antwort so hat es nun funktioniert
Java:
        JDA jda = null;
        try {
            jda = builder.buildBlocking();
        } catch (LoginException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (jda != null) ;
        new CommandSettings("f!", jda)
                .add("believe", new Broadcast())
                .add("way", new Broadcast())
                .activate();
    }
 
Ähnliche Java Themen

Ähnliche Java Themen

Neue Themen


Oben