D
D
dolen2282020-05-11 10:11:18
MySQL
dolen228, 2020-05-11 10:11:18

At attempt to receive value from Map returns null. How to fix?

Hello, I'm taking values ​​from MySQL and storing them in a Map, but it returns null when I try to get the value.

Here is the code:

private static Map<Player, Map<String, Integer>> cache = new HashMap<>();

    public static void cache(Player player) {
        Bukkit.getScheduler().runTaskAsynchronously(Jumper.getPlugin(Jumper.class), () -> {
            Connection connection = Jumper.connection;
            PreparedStatement statement;
            try {
                statement = connection.prepareStatement("SELECT * FROM players WHERE nickname = ?");
                statement.setString(1, player.getName());
                ResultSet rs = statement.executeQuery();
                Map<String, Integer> map = new HashMap<>();
                while (rs.next()) {
                    if(cache.get(player) != null) {
                        map.replace("level", rs.getInt("level"));
                        map.replace("xp", rs.getInt("xp"));
                        map.replace("balance", rs.getInt("balance"));
                        map.replace("rebirths", rs.getInt("rebirths"));
                        map.replace("bLevel", rs.getInt("bLevel"));
                    } else {
                        map.put("level", rs.getInt("level"));
                        map.put("xp", rs.getInt("xp"));
                        map.put("balance", rs.getInt("balance"));
                        map.put("rebirths", rs.getInt("rebirths"));
                        map.put("bLevel", rs.getInt("bLevel"));
                    }
                }
                cache.put(player, map);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });
    }

    public static Integer getLevel(Player player) throws SQLException {
        return cache.get(player).get("level");
    }


How can this be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dolen228, 2020-05-11
@dolen228

It's decided. I just made delays in getting

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question