Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
My MongoDB .find() method is returning null even thought the data exists

I am trying to find a user's UUID in my database, but it's returning null for no reason. It's in the connect() method. I can find this manually, and in my project in javaScript. But for some reason, I'm getting this error.

public final class DiscordLink extends JavaPlugin implements Listener {
public MongoCollection<Document> users;
public MongoDatabase mongoDatabase;
    @Override
    public void onEnable() {
        System.setProperty("DEBUG.GO", "true");
        System.setProperty("DB.TRACE", "true");
        Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
        mongoLogger.setLevel(Level.WARNING);
        connect();

        getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[DiscordLink] Plugin enabled...");

        getServer().getPluginManager().registerEvents(this, this);

    }

    @Override
    public void onDisable() {
        getServer().getConsoleSender().sendMessage(ChatColor.RED + "[DiscordLink] Plugin disabled...");
    }

    public void connect() {
        MongoClient mongoClient = MongoClients.create("MONGOURI");
        MongoDatabase mongoDatabase = mongoClient.getDatabase("myFirstDatabase");
        MongoCollection<Document> users = mongoDatabase.getCollection("users");
        Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[DiscordLink] Connected to database!");
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        Document document = users.find(Filters.eq("mcUUID", player.getUniqueId().toString())).first();
        if(document != null) {
            player.kickPlayer(ChatColor.RED + "Discord Authentication Failed!");
        } else { return; }

Error:

[15:43:01 ERROR]: Could not pass event PlayerJoinEvent to DiscordLink v1.0-SNAPSHOT
java.lang.NullPointerException: Cannot invoke "com.mongodb.client.MongoCollection.find(org.bson.conversions.Bson)" because "this.users" is null
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In the connect method, you are declaring the field MongoCollection<Document> users.

If you IDE is well configurated, it should tell you that this field is not used. Here, you are creating a local field (that keep only in the method scope) instead of setting the global field, which is declared just below the public final class DiscordLink.

To fix it, do like this :

this.mongoDatabase = mongoClient.getDatabase("myFirstDatabase")
this.users = this.mongoDatabase.getCollection("users");

Here, the this key-word is to say you are looking for the global variable, declared outside of the method.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda