Error occurred while enabling (Is it up to date?)

Discussion in 'Plugin Development' started by Furry, May 13, 2021.

  1. Offline

    Furry

    There is such an error plugin
    Message in my console


    Code:
     Error occurred while enabling fSklep v1.6 (Is it up to date?)
    java.lang.NullPointerException
    at pl.furry.shop.data.UserManager.loadUsers(UserManager.java:69) ~[?:?]
    at pl.furry.shop.Main.registerManager(Main.java:99) ~[?:?]
    at pl.furry.shop.Main.onEnable(Main.java:48) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[server.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [server.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [server.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [server.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [server.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [server.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [server.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [server.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [server.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [server.jar:git-Spigot-21fe707-e1ebe52]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_202]
    Line 99
    Code:java
    1. public void registerManager() {
    2. UserManager.loadUsers();
    3. }

    Line 69
    Code:java
    1.  
    2. package pl.furry.shop.data;
    3.  
    4. import java.util.concurrent.*;
    5. import org.bukkit.entity.*;
    6.  
    7. import pl.furry.shop.*;
    8. import pl.furry.shop.managers.*;
    9. import pl.furry.shop.utils.*;
    10.  
    11. import java.sql.*;
    12.  
    13. public class UserManager
    14. {
    15. private static final ConcurrentHashMap<String, User> users;
    16.  
    17. static {
    18. users = new ConcurrentHashMap<String, User>();
    19. }
    20.  
    21. public static User getUser(final String name) {
    22. for (final User u : UserManager.users.values()) {
    23. if (u.getName().equalsIgnoreCase(name)) {
    24. return u;
    25. }
    26. }
    27. return null;
    28. }
    29.  
    30. public static User getUser(final Player p) {
    31. for (final User u : UserManager.users.values()) {
    32. if (u.getName().equalsIgnoreCase(p.getName())) {
    33. return u;
    34. }
    35. }
    36. return null;
    37. }
    38.  
    39. public static void createrUser(final Player p) {
    40. final User u = new User(p);
    41. UserManager.users.put(p.getName(), u);
    42. CoinManager.addCoins(u);
    43. AllCoinsManager.addAllCoins(u);
    44. SpendCoinsManager.addSpendCoins(u);
    45. TimeManager.addTime(u);
    46. }
    47.  
    48. public static void createrUser(final String p) {
    49. final User u = new User(p);
    50. UserManager.users.put(p, u);
    51. CoinManager.addCoins(u);
    52. AllCoinsManager.addAllCoins(u);
    53. SpendCoinsManager.addSpendCoins(u);
    54. TimeManager.addTime(u);
    55. }
    56.  
    57. public static void deleteUser(final User u) {
    58. UserManager.users.remove(u.getName());
    59. CoinManager.removeCoins(u);
    60. AllCoinsManager.removeAllCoins(u);
    61. SpendCoinsManager.addSpendCoins(u);
    62. TimeManager.removeTime(u);
    63. Main.getStore().update(false, "DELETE FROM `{P}users` WHERE `name` = '" + u.getName() + "'");
    64. }
    65.  
    66. public static void loadUsers() {
    67. try {
    68. final ResultSet rs = Main.getStore().query("SELECT * FROM `{P}users`");
    69. while (rs.next()) {
    70. final User u = new User(rs);
    71. UserManager.users.put(u.getName(), u);
    72. CoinManager.addCoins(u);
    73. AllCoinsManager.addAllCoins(u);
    74. SpendCoinsManager.addSpendCoins(u);
    75. TimeManager.addTime(u);
    76. }
    77. rs.close();
    78. Logger.info("Loaded " + UserManager.users.size() + " players");
    79. }
    80. catch (SQLException e) {
    81. Logger.info("Can not load players Error " + e.getMessage());
    82. e.printStackTrace();
    83. }
    84. }
    85.  
    86. public static ConcurrentHashMap<String, User> getUsers() {
    87. return UserManager.users;
    88. }
    89. }
     
    Last edited: May 13, 2021
  2. Offline

    Wick

    UserManager is probably null, can you share all relevant classes?
     
  3. Offline

    Furry

    Code:java
    1.  
    2. package pl.furry.shop.data;
    3.  
    4. import java.util.concurrent.*;
    5. import org.bukkit.entity.*;
    6.  
    7. import pl.furry.shop.*;
    8. import pl.furry.shop.managers.*;
    9. import pl.furry.shop.utils.*;
    10.  
    11. import java.sql.*;
    12.  
    13. public class UserManager
    14. {
    15. private static final ConcurrentHashMap<String, User> users;
    16.  
    17. static {
    18. users = new ConcurrentHashMap<String, User>();
    19. }
    20.  
    21. public static User getUser(final String name) {
    22. for (final User u : UserManager.users.values()) {
    23. if (u.getName().equalsIgnoreCase(name)) {
    24. return u;
    25. }
    26. }
    27. return null;
    28. }
    29.  
    30. public static User getUser(final Player p) {
    31. for (final User u : UserManager.users.values()) {
    32. if (u.getName().equalsIgnoreCase(p.getName())) {
    33. return u;
    34. }
    35. }
    36. return null;
    37. }
    38.  
    39. public static void createrUser(final Player p) {
    40. final User u = new User(p);
    41. UserManager.users.put(p.getName(), u);
    42. CoinManager.addCoins(u);
    43. AllCoinsManager.addAllCoins(u);
    44. SpendCoinsManager.addSpendCoins(u);
    45. TimeManager.addTime(u);
    46. }
    47.  
    48. public static void createrUser(final String p) {
    49. final User u = new User(p);
    50. UserManager.users.put(p, u);
    51. CoinManager.addCoins(u);
    52. AllCoinsManager.addAllCoins(u);
    53. SpendCoinsManager.addSpendCoins(u);
    54. TimeManager.addTime(u);
    55. }
    56.  
    57. public static void deleteUser(final User u) {
    58. UserManager.users.remove(u.getName());
    59. CoinManager.removeCoins(u);
    60. AllCoinsManager.removeAllCoins(u);
    61. SpendCoinsManager.addSpendCoins(u);
    62. TimeManager.removeTime(u);
    63. Main.getStore().update(false, "DELETE FROM `{P}users` WHERE `name` = '" + u.getName() + "'");
    64. }
    65.  
    66. public static void loadUsers() {
    67. try {
    68. final ResultSet rs = Main.getStore().query("SELECT * FROM `{P}users`");
    69. while (rs.next()) {
    70. final User u = new User(rs);
    71. UserManager.users.put(u.getName(), u);
    72. CoinManager.addCoins(u);
    73. AllCoinsManager.addAllCoins(u);
    74. SpendCoinsManager.addSpendCoins(u);
    75. TimeManager.addTime(u);
    76. }
    77. rs.close();
    78. Logger.info("Loaded " + UserManager.users.size() + " players");
    79. }
    80. catch (SQLException e) {
    81. Logger.info("Can not load players Error " + e.getMessage());
    82. e.printStackTrace();
    83. }
    84. }
    85.  
    86. public static ConcurrentHashMap<String, User> getUsers() {
    87. return UserManager.users;
    88. }
    89. }

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.LOW)
    3. public void onJoin(final PlayerJoinEvent e) {
    4. e.setJoinMessage((String)null);
    5. final Player p = e.getPlayer();
    6. final User u = UserManager.getUser(p);
    7. if (u == null) {
    8. UserManager.createrUser(p);
    9. }
    10. }
    11. }
     
    Last edited: May 13, 2021
  4. Offline

    KarimAKL

    @Furry We need line 69 of UserManager, not line 99 of Main.

    UserManager.loadUsers() is a static method, so nothing can be null in the code OP posted.
     
  5. Offline

    Furry

  6. Offline

    Newdel

    Code:
    Main.getStore()
    
    Dunno what it does, but it definitly doesn't make sense to store a DBHandler in the Main class and use it in a DBHelper class

    I don't understand what you are trying to do...Users won't get stored in the database but some methods are executing SQL queries...or at least try to
     
  7. Offline

    davidclue

    Code:
    final User u = new User(rs);
    Share the method that creates the user since this is the line giving the error.
     
  8. Offline

    Furry

    @davidclue
    Code:
        @EventHandler(priority = EventPriority.LOW)
        public void onJoin(final PlayerJoinEvent e) {
            e.setJoinMessage((String)null);
            final Player p = e.getPlayer();
            final User u = UserManager.getUser(p);
            if (u == null) {
                UserManager.createrUser(p);
            }
        }
    }
     
  9. Offline

    KarimAKL

    @Furry The stack trace does not match with the code. Please give us the same source code that generates the stack trace.
     
  10. Offline

    davidclue

    @Furry No, share the method from your User class it will look something like
    Code:
    public User(String p) {
    
    }
     
  11. Offline

    Newdel

    Why should it?

    Where is the mismatch?
     
  12. Offline

    KarimAKL

    Code:Java
    1. final User u = new User(rs);

    This line cannot throw an exception, so the source code and the stack trace do not match.
    Please compile your current code, recreate the stack trace, then send us the code and the stack trace.
     
  13. Offline

    Newdel

    The stacktrace never said that this line threw the exception...
     
  14. Offline

    KarimAKL

    Code:Java
    1. at pl.furry.shop.data.UserManager.loadUsers(UserManager.java:69)

    Line 69 in the UserManager.java file. According to the code you posted, that is line 69:
     
  15. Offline

    davidclue

    That's the line throwing an error and probably in the constructor he is calling a static method that is null.
     
  16. Offline

    Newdel

    Code he posted shows "69. 1.while (rs.next()) {" for me

    Edit: There would be another line in the stacktrace above the one you mentioned which shows the constructor and the line in the constructor where the error was thrown
     
  17. Offline

    KarimAKL

    1. A method cannot be null, you probably meant that it returns null and that @Furry is trying to do something with it.
    2. The stack trace would tell us that if that was the case, which it does not.
     
  18. Offline

    davidclue

    @KarimAKL Yeah nvm just realized that, so then the stack trace doesn't match the code at all.

    @Newdel he put a space at the very top for line 1 which does not count in the code, he must've pressed enter when he was pasting it in by accident.
     
  19. Offline

    KarimAKL

    Exactly.
    @Furry, we need you to post code that matches the stack trace.

    I fixed it when I quoted the code earlier. The problem is the way the code blocks work on these forums.
    @Furry used the code blocks like this:

    [Syntax=Java]
    // First line here[/Syntax]

    Instead of this:

    [Syntax=Java]// First line here[/Syntax]
     
  20. Offline

    Newdel

    Maybe the empty first line was intended. Or the code is completely different from the stacktrace but as I said before:
    Code:
    final User u = new User(rs);
    is not throwing the exception
     

Share This Page