Error creating a plugin that counts the player logins (SQL)

Discussion in 'Plugin Development' started by mig4ng, Oct 4, 2013.

Thread Status:
Not open for further replies.
  1. Hi!
    I'm doing some tests and trying to learn SQL plugin development and today I got a error of NullPoint and I can't find the source of the problem.

    Plugin Code:
    Code:java
    1. package net.madnature.madstorehub;
    2.  
    3. import java.sql.Connection;
    4. import java.sql.DriverManager;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7. import java.sql.SQLException;
    8.  
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.PlayerLoginEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class main extends JavaPlugin implements Listener {
    16.  
    17. private static Connection connection;
    18.  
    19. public void onEnable(){
    20. getServer().getPluginManager().registerEvents(this, this);
    21. }
    22.  
    23. public void onDisable(){
    24.  
    25. try {
    26. if(connection != null && !connection.isClosed()) {
    27.  
    28. }
    29. } catch (SQLException e) {
    30. e.printStackTrace();
    31. }
    32.  
    33. }
    34.  
    35. public synchronized static void openConnection() {
    36. try{
    37. connection.close();
    38. } catch(Exception e) {
    39.  
    40. }
    41. }
    42.  
    43. public synchronized static void closeConnection() {
    44. try{
    45. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/madnature", "root", "NOPE!");
    46. } catch(Exception e) {
    47.  
    48. }
    49. }
    50.  
    51. public synchronized static boolean playerDataContainsPlayer(Player player){
    52. try{
    53. PreparedStatement sql = connection
    54. .prepareStatement("SELECT * FROM users WHERE username=?;");
    55. sql.setString(1, player.getName());
    56. ResultSet resultSet = sql.executeQuery();
    57. boolean containsPlayer = resultSet.next();
    58.  
    59. sql.close();
    60. resultSet.close();
    61.  
    62. return containsPlayer;
    63. } catch(Exception e){
    64. e.printStackTrace();
    65. return false;
    66. }
    67. }
    68.  
    69. @EventHandler
    70. public void onPlayerLogin(PlayerLoginEvent event) {
    71.  
    72. try{
    73. openConnection();
    74. } catch(Exception e) {
    75. e.printStackTrace();
    76. }
    77. try {
    78. int previousLogins = 0;
    79.  
    80. if (playerDataContainsPlayer(event.getPlayer())){
    81. PreparedStatement sql = connection.prepareStatement("SELECT login FROM users WHERE username=?;");
    82. sql.setString(1, event.getPlayer().getName());
    83.  
    84. ResultSet result = sql.executeQuery();
    85. result.next();
    86.  
    87. previousLogins = result.getInt("logins");
    88.  
    89. PreparedStatement loginsUpdate = connection.prepareStatement("UPDATE users SET logins=? WHERE username=?;");
    90. loginsUpdate.setInt(1, previousLogins + 1);
    91. loginsUpdate.setString(2, event.getPlayer().getName());
    92. loginsUpdate.executeUpdate();
    93.  
    94. loginsUpdate.close();
    95. sql.close();
    96. result.close();
    97. } else {
    98. PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO users values(?,0,1);");
    99. newPlayer.setString(1, event.getPlayer().getName());
    100. newPlayer.execute();
    101. newPlayer.close();
    102. }
    103. } catch(Exception e){
    104. e.printStackTrace();
    105. } finally {
    106. closeConnection();
    107. }
    108.  
    109. }
    110.  
    111. }
    112.  


    Whenever I try to login, I get this error:
    Code:
    2013-10-01 20:24:55 [SEVERE] java.lang.NullPointerException
    2013-10-01 20:24:55 [SEVERE] at net.madnature.madstorehub.main.playerDataContainsPlayer(main.java:53)
    2013-10-01 20:24:55 [SEVERE] at net.madnature.madstorehub.main.onPlayerLogin(main.java:79)
    2013-10-01 20:24:55 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2013-10-01 20:24:55 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    2013-10-01 20:24:55 [SEVERE] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    2013-10-01 20:24:55 [SEVERE] at java.lang.reflect.Method.invoke(Method.java:606)
    2013-10-01 20:24:55 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    2013-10-01 20:24:55 [SEVERE] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    2013-10-01 20:24:55 [SEVERE] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    2013-10-01 20:24:55 [SEVERE] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.PlayerList.attemptLogin(PlayerList.java:335)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.PendingConnection.e(PendingConnection.java:123)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.PendingConnection.d(PendingConnection.java:43)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:41)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:29)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    2013-10-01 20:24:55 [SEVERE] java.lang.NullPointerException
    2013-10-01 20:24:55 [SEVERE] at net.madnature.madstorehub.main.onPlayerLogin(main.java:97)
    2013-10-01 20:24:55 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2013-10-01 20:24:55 [SEVERE] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    2013-10-01 20:24:55 [SEVERE] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    2013-10-01 20:24:55 [SEVERE] at java.lang.reflect.Method.invoke(Method.java:606)
    2013-10-01 20:24:55 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    2013-10-01 20:24:55 [SEVERE] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    2013-10-01 20:24:55 [SEVERE] at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    2013-10-01 20:24:55 [SEVERE] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.PlayerList.attemptLogin(PlayerList.java:335)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.PendingConnection.e(PendingConnection.java:123)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.PendingConnection.d(PendingConnection.java:43)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:41)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:29)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:590)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    2013-10-01 20:24:55 [SEVERE] at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    2013-10-01 20:24:56 [INFO] mig4ng[/192.168.1.6:50188] logged in with entity id 106 at ([Main] 31.971619617727896, 65.0, 266.4128168083152)
    Do any of you know how to fix this?
    Thanks
     
  2. Offline

    adam753

    You are actually getting two different consecutive errors there. The first one is on line 53 of main.java, and the second is on line 97 of main.java. It looks like the lines in your code haven't numbered properly here, so can you show which lines your IDE says are lines 53 and 97?

    But, at a glance, it looks like you're using the "connection" variable without initializing it.
     
  3. 53:
    Code:java
    1. PreparedStatement sql = connection
    2. .prepareStatement("SELECT * FROM users WHERE username=?;");


    97:
    Code:java
    1. PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO users values(?,0,1);");


    I'm a fucking tard xD, You can close this :)

    Code:java
    1. public synchronized static void openConnection() {
    2. try{
    3. connection.close();
    4. } catch(Exception e) {
    5.  
    6. }
    7. }

    That's why you shoulnd't copy paste from your own code.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  4. Offline

    GaaTavares

    I know it's funny but you did awesome: helped me a lot!! I was having lag issues with mysql but now it's fine ;D
     
  5. How did I help you btw? xD
     
  6. Offline

    GaaTavares

    I wasn't using the "synchronized" before the voids, booleans, and this probably will reduce the lag..
    mig4ng
     
    mig4ng likes this.
  7. Offline

    CraftBang

    Nice to share the fix :)
    Will also help other people if they run through a problem
     
Thread Status:
Not open for further replies.

Share This Page