即使数据存在,我的 MongoDB .find() 方法也会返回 null
My MongoDB .find() method is returning null even thought the data exists
我试图在我的数据库中查找用户的 UUID,但它无缘无故地返回 null。它在 connect() 方法中。我可以在 javaScript 的项目中手动找到它。但出于某种原因,我收到此错误。
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; }
错误:
[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
在 connect
方法中,您正在声明字段 MongoCollection<Document> users
。
如果你IDE配置好,应该会告诉你这个字段没有用到。在这里,您正在创建一个局部字段(仅保留在方法范围内)而不是设置全局字段,该字段在 public final class DiscordLink
.
下方声明
要修复它,请这样做:
this.mongoDatabase = mongoClient.getDatabase("myFirstDatabase")
this.users = this.mongoDatabase.getCollection("users");
这里,this
关键字是说你正在寻找在方法之外声明的全局变量。
我试图在我的数据库中查找用户的 UUID,但它无缘无故地返回 null。它在 connect() 方法中。我可以在 javaScript 的项目中手动找到它。但出于某种原因,我收到此错误。
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; }
错误:
[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
在 connect
方法中,您正在声明字段 MongoCollection<Document> users
。
如果你IDE配置好,应该会告诉你这个字段没有用到。在这里,您正在创建一个局部字段(仅保留在方法范围内)而不是设置全局字段,该字段在 public final class DiscordLink
.
要修复它,请这样做:
this.mongoDatabase = mongoClient.getDatabase("myFirstDatabase")
this.users = this.mongoDatabase.getCollection("users");
这里,this
关键字是说你正在寻找在方法之外声明的全局变量。