In my code @ Line 50, where I insert my data into my database I keep getting a NPE, and can't seem to figure out why Code: package net.thedemise.teams.commands; import com.mongodb.BasicDBObject; import net.thedemise.teams.MongoManager; import net.thedemise.teams.PlayerData; import net.thedemise.teams.TeamData; import org.bson.BSONObject; import org.bson.Document; import org.bson.types.ObjectId; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.HashMap; import java.util.UUID; public class TeamCommand implements CommandExecutor { private MongoManager mongoManager; public MongoManager getMongoManager() { return mongoManager; } @Override public boolean onCommand(CommandSender commandSender, Command command, String string, String[] args) { if (command.getName().equalsIgnoreCase("team")) { Player player = (Player) commandSender; if(commandSender instanceof Player){ if (args.length == 0) { player.sendMessage("Team help: "); player.sendMessage("team create"); } else { if (args[0].equalsIgnoreCase("create")) { player.sendMessage(ChatColor.BLUE + "Please do /team create <name>"); if (args.length >= 2) { StringBuilder message = new StringBuilder(); for (int i = 1; i < args.length; i++) { message.append(" ").append(args); } String msg = message.toString(); ObjectId id = new ObjectId("4f693d40e4b04cde19f17205"); BasicDBObject obj = new BasicDBObject(); obj.append("_id", id); HashMap<UUID, TeamData> teamDataHashMap = new HashMap<>(); BasicDBObject query = new BasicDBObject(); query.putAll((BSONObject) query); Document teamdoc = new Document("_id", id); System.out.println("Team created"); player.sendMessage(msg + " Has been created!"); teamdoc.append("NAME", message.toString()); teamdoc.append("owner", player.getName().toString()); getMongoManager().players.insertOne(teamdoc); teamDataHashMap.put(player.getUniqueId(), new TeamData(message.toString(), player.getName())); } return true; } } }else{ player.sendMessage("Only players can use this command!"); return true; } } return false; } }
Include some line numbers. If it is this line: PHP: getMongoManager().players.insertOne(teamdoc); then my guess would be players is null or getMongoManager() is returning null. Add some debugs like: System.out.println(getMongoManager() == null) & players == null
I'd have to agree, I do not see where you initialize mongoManager. It is not static either, so I believe the way its currently written, it will always be null. I do not know how your mongoManager class works but you should initialize it, either with Code: private MongoManager mongoManager = new MongoManager(/*params*/); or in the constructor for your commandExecutor (make one) Code: public TeamCommand(/*params*/) { //Stuff mongoManager = new MongoManager(/*params*/); //More stuff }