[MySQL]What's wrong here?

Discussion in 'Plugin Development' started by Pitazzo, Nov 26, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Pitazzo

    When I'm trying to use this method, I allway get an error in the console; here is the code:

    Code:java
    1. package es.programahermes.pitazzo;
    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.  
    11. public class MySQL {
    12.  
    13. public static Connection connection;
    14.  
    15. public synchronized static void openConnection() {
    16. try {
    17.  
    18. connection = DriverManager.getConnection(
    19. "jdbc:mysql://localhost:3306/bukkit", "root", "");
    20.  
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. }
    24.  
    25. }
    26.  
    27. synchronized static boolean dbContanisPlayer(Player player) {
    28. openConnection();
    29. try {
    30.  
    31. PreparedStatement ps = connection
    32. .prepareStatement("SELECT * FROM `bukkit`.`user_data` WHERE name=?;");
    33. ps.setString(1, player.getName());
    34. ResultSet resultSet = ps.executeQuery();
    35. boolean containsPlayer = resultSet.next();
    36.  
    37. ps.close();
    38. resultSet.close();
    39.  
    40. return containsPlayer;
    41.  
    42. } catch (Exception e) {
    43. e.printStackTrace();
    44. return false;
    45. }
    46.  
    47. }
    48.  
    49. synchronized static void firstJoin(Player player) {
    50. openConnection();
    51.  
    52. try {
    53. PreparedStatement ps = connection
    54. .prepareStatement("INSERT INTO `user_data`(`name`, `points`) VALUES (?,0)");
    55. ps.setString(1, player.getName());
    56. ps.executeUpdate();
    57. ps.close();
    58. } catch (SQLException e) {
    59. e.printStackTrace();
    60. }
    61.  
    62. }
    63.  
    64. synchronized static int getPoints(Player player) {
    65.  
    66. try {
    67. PreparedStatement sql = connection
    68. .prepareStatement("SELECT points FROM `bukkit`.`user_data` WHERE name=?;");
    69.  
    70. sql.setString(1, player.getName());
    71. ResultSet result = sql.executeQuery();
    72. result.next();
    73. int points = result.getInt("points");
    74. sql.close();
    75. result.close();
    76.  
    77. return points;
    78.  
    79. } catch (SQLException e) {
    80. e.printStackTrace();
    81. }
    82. return 0;
    83.  
    84. }
    85.  
    86. synchronized static int getLevel(Player player) {
    87.  
    88. try {
    89.  
    90. PreparedStatement ps = connection
    91. .prepareStatement("SELECT nivel1 FROM `bukkit`.`user_data` WHERE name=?");
    92. ps.setString(1, player.getName());
    93. ResultSet result = ps.executeQuery();
    94. result.next();
    95. int nivel = result.getInt("nivel1");
    96. return nivel;
    97.  
    98. } catch (Exception e) {
    99. e.printStackTrace();
    100. }
    101.  
    102. return 0;
    103.  
    104. }
    105.  
    106. synchronized static void addPoints(Player player, int newPoints) {
    107. try {
    108.  
    109. PreparedStatement ps1 = connection
    110. .prepareStatement("SELECT points FROM `bukkit`.`user_data` WHERE name=?");
    111. ps1.setString(1, player.getName());
    112. ResultSet result1 = ps1.executeQuery();
    113. result1.next();
    114. int previousPoints = result1.getInt("points");
    115.  
    116. PreparedStatement ps2 = connection
    117. .prepareStatement("UPDATE `user_data` SET `points`=? WHERE name=?");
    118. ps2.setString(2, player.getName());
    119. ps2.setInt(1, newPoints + previousPoints);
    120. ps2.executeUpdate();
    121. ps1.close();
    122. result1.close();
    123. ps2.close();
    124.  
    125. } catch (Exception e) {
    126. e.printStackTrace();
    127. }
    128.  
    129. }
    130.  
    131. }
    132.  


    I'm having problems with the addPoints() method; I get this error in the console: http://gyazo.com/17d5d4b84e02e6c63bee7da0daa83a45

    Thanks in advance
     
  2. Offline

    pope_on_dope

    what line is the null pointer at?
     
  3. Offline

    NathanWolf

    Have you called openConnection first? I looks like "connection" will be null if you do not.

    It's best to add null checks for things like that and then toss an Exception with a more descriptive message (e.g. "addPoints called, but connection is not open").

    EDIT: To preach a general favorite rule of mine - this particularly applies to NPE's, but any unexpected error should be treated this way IMO.

    If you get an error that you didn't expect, FIRST go to the source of the code, and catch the error. Log a better error message if appropriate, return a failure value to the caller, throw an exception- something that shows you where to look next time this happens again.

    After doing that (and testing that your new error handler works), THEN go look for the root of the problem and fix it.

    Just my 2c, but following this method has helped me make robust code that is easier to debug than if you just write code that assumes parameters are always valid, that kind of thing- but you also don't have to go crazy null-checking every single thing (well, not necessarily, anyway)
     
    Pitazzo likes this.
  4. Offline

    Darq

    It looks like the Connection object is null when you call the addPoints method.

    Try something like:
    Code:
    if (connection == null) {
        openConnection();
    }
    
     
    Pitazzo likes this.
  5. Offline

    Pitazzo

    Darq Yep, that's a good idea! Thanks :)
    NathanWolf Yeah!! Fixed! Thanks a lot :D And thabks for the advices too, they were really useful :)

    But now there is a new problem... When I add points to someone with the method addPoints(), the points are added twice! (lol?) E.g. If I want to add 5 points, the plugins gives me 10... surprising... Any idea?

    Thanks in advance!

    Any idea?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  6. Divide it by two? xD
     
  7. Offline

    Pitazzo

    Hahahahaha I had already tought it! But not too smart xDD

    OK, I've just found WHERE is the problem but not WHICH is it... maddening.
    If I do this...
    Code:
    @EventHandler
        public void OnPlayerPlace(BlockPlaceEvent event){
            Player player = event.getPlayer();
            System.out.println("Hey!");
           
        }
    I get that "Hey!" twice in my log!! Wtf?? Any idea??
     
  8. Pitazzo
    Did you register your listeners twice?
     
  9. Offline

    Pitazzo


    May be... I have to confess that I jst copied this from a tutorial of multiples classes... Not sure what it does :(

    Code:java
    1. public class Main extends JavaPlugin implements CommandExecutor, Listener {
    2.  
    3. private static Plugin plugin;
    4.  
    5. public void onEnable() {
    6. plugin = this;
    7. registerEvents(this, new Listeners());
    8.  
    9. }
    10.  
    11. public void onDisable() {
    12. plugin = null;
    13.  
    14. }
    15.  
    16. public static void registerEvents(org.bukkit.plugin.Plugin plugin,
    17. Listener... listeners) {
    18. for (Listener listener : listeners) {
    19. Bukkit.getServer().getPluginManager()
    20. .registerEvents(listener, plugin);
    21. }
    22. }
    23.  
    24. public static Plugin getPlugin() {
    25. return plugin;
    26. }
     
  10. Pitazzo
    1. Why are you implementing CommandExecutor in your main class. You only use that if you want to use commands in classes other than your main class.
    2. To register your listeners, simply refer to this: http://wiki.bukkit.org/Event_API_Reference#Registering_Events

    EDIT
    Also, you don't need to get an instance of your plugin privatestatic Plugin plugin;
    and then set it equal to things in your onEnable() and onDisable() methods. You can delete those.
     
    Pitazzo likes this.
  11. Offline

    Pitazzo


    There is a command in my main class :)

    I did everything that you suggested, here is the result:

    Code:java
    1. package es.programahermes.pitazzo;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.BlockBreakEvent;
    13. import org.bukkit.event.block.BlockPlaceEvent;
    14. import org.bukkit.plugin.Plugin;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. import es.programahermes.pitazzo.MySQL;
    18.  
    19.  
    20.  
    21. public class Main extends JavaPlugin implements CommandExecutor, Listener {
    22.  
    23. MySQL MySQL = new MySQL();
    24.  
    25. public void onEnable() {
    26.  
    27. getServer().getPluginManager().registerEvents(this, this);
    28.  
    29. }
    30.  
    31. public void onDisable() {
    32.  
    33.  
    34. }
    35.  
    36. @EventHandler
    37. public void onBlockBreak(BlockBreakEvent event){
    38. Player player = event.getPlayer();
    39.  
    40. if(event.getBlock().getType().equals(Material.STONE)){
    41.  
    42. MySQL.addPoints(player, 2);
    43. }
    44.  
    45. }
    46.  
    47.  
    48. public boolean onCommand(CommandSender sender, Command cmd, String label,
    49. String[] args) {
    50. if (cmd.getName().equalsIgnoreCase("puntos")) {
    51. Player player = (Player) sender;
    52. int points = MySQL.getPoints(player);
    53.  
    54. player.sendMessage(ChatColor.GREEN+"Tienes "+points+" puntos");
    55. }
    56. return false;
    57. }
    58.  
    59.  
    60.  
    61.  
    62.  
    63. }
    64.  


    But it still not work...

    Thanks for yoy replies!
     
  12. Offline

    Darq

    Your command won't work, it has to be registered :)

    Assuming you already have the command "puntos" in your plugin.yml, you can call this in your onEnable():
    Code:
    getCommand("puntos").setExecutor(this);
    
     
  13. Pitazzo
    If your command is in your main class you do not need to use CommandExecutor. Get rid of it or your command won't work. You can do what Darq suggested, but it's completely unnecessary.
     
  14. Offline

    Pitazzo

    Darq The Gaming Grunts Thank you both, but mi command works perfectly xDDD

    What I can't understand is why the hell when I run this in my plugin, I get two Hey!s

    Code:java
    1. @EventHandler
    2. public void onBlockBreak(BlockBreakEvent event){
    3. Player player = event.getPlayer();
    4.  
    5. if(event.getBlock().getType().equals(Material.STONE)){
    6.  
    7.  
    8. player.sendMessage("Hey");
    9. }
    10.  
    11. }
     
  15. Pitazzo
    I don't understand what your problem is...
     
  16. Offline

    Pitazzo

  17. Pitazzo
    Post your full code plz
     
  18. Offline

    Pitazzo

    The Gaming Grunts
    Here it is:

    Main Class:
    Code:java
    1. package es.programahermes.pitazzo;
    2.  
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.BlockBreakEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. import es.programahermes.pitazzo.MySQL;
    16.  
    17.  
    18.  
    19. public class Main extends JavaPlugin implements CommandExecutor, Listener {
    20.  
    21. MySQL MySQL = new MySQL();
    22.  
    23. public void onEnable() {
    24.  
    25. getServer().getPluginManager().registerEvents(this, this);
    26.  
    27. }
    28.  
    29. public void onDisable() {
    30.  
    31.  
    32. }
    33.  
    34. @EventHandler
    35. public void onBlockBreak(BlockBreakEvent event){
    36. Player player = event.getPlayer();
    37.  
    38. if(event.getBlock().getType().equals(Material.STONE)){
    39. MySQL.addPoints(player, 2);
    40. }
    41.  
    42. }
    43.  
    44.  
    45. public boolean onCommand(CommandSender sender, Command cmd, String label,
    46. String[] args) {
    47. if (cmd.getName().equalsIgnoreCase("puntos")) {
    48. Player player = (Player) sender;
    49. int points = MySQL.getPoints(player);
    50.  
    51. player.sendMessage(ChatColor.GREEN+"Tienes "+points+" puntos");
    52. }
    53. return false;
    54. }
    55.  
    56.  
    57.  
    58.  
    59.  
    60. }
    61.  


    MySQL Class:

    Code:java
    1. package es.programahermes.pitazzo;
    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.  
    11. public class MySQL {
    12.  
    13. public static Connection connection;
    14.  
    15. public synchronized static void openConnection() {
    16. try {
    17.  
    18. connection = DriverManager.getConnection(
    19. "jdbc:mysql://localhost:3306/bukkit", "root", "");
    20.  
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. }
    24.  
    25. }
    26.  
    27. public synchronized static void closeConnection() {
    28.  
    29. try {
    30.  
    31. connection.close();
    32.  
    33. } catch (Exception e) {
    34. e.printStackTrace();
    35. }
    36. }
    37.  
    38. synchronized static boolean dbContanisPlayer(Player player) {
    39. openConnection();
    40. try {
    41.  
    42. PreparedStatement ps = connection
    43. .prepareStatement("SELECT * FROM `bukkit`.`user_data` WHERE name=?;");
    44. ps.setString(1, player.getName());
    45. ResultSet resultSet = ps.executeQuery();
    46. boolean containsPlayer = resultSet.next();
    47.  
    48. ps.close();
    49. resultSet.close();
    50. closeConnection();
    51. return containsPlayer;
    52.  
    53. } catch (Exception e) {
    54. e.printStackTrace();
    55. return false;
    56. }
    57.  
    58. }
    59.  
    60. synchronized static void firstJoin(Player player) {
    61. openConnection();
    62.  
    63. try {
    64. PreparedStatement ps = connection
    65. .prepareStatement("INSERT INTO `user_data`(`name`, `points`) VALUES (?,0)");
    66. ps.setString(1, player.getName());
    67. ps.executeUpdate();
    68. ps.close();
    69. closeConnection();
    70. } catch (SQLException e) {
    71. e.printStackTrace();
    72. }
    73.  
    74. }
    75.  
    76. synchronized static int getPoints(Player player) {
    77. openConnection();
    78. try {
    79. PreparedStatement sql = connection
    80. .prepareStatement("SELECT points FROM `bukkit`.`user_data` WHERE name=?;");
    81.  
    82. sql.setString(1, player.getName());
    83. ResultSet result = sql.executeQuery();
    84. result.next();
    85. int points = result.getInt("points");
    86. sql.close();
    87. result.close();
    88. closeConnection();
    89. return points;
    90.  
    91. } catch (SQLException e) {
    92. e.printStackTrace();
    93. }
    94. return 0;
    95.  
    96. }
    97.  
    98. synchronized static int getLevel(Player player) {
    99. openConnection();
    100. try {
    101.  
    102. PreparedStatement ps = connection
    103. .prepareStatement("SELECT nivel1 FROM `bukkit`.`user_data` WHERE name=?");
    104. ps.setString(1, player.getName());
    105. ResultSet result = ps.executeQuery();
    106. result.next();
    107. int nivel = result.getInt("nivel1");
    108. closeConnection();
    109. return nivel;
    110.  
    111. } catch (Exception e) {
    112. e.printStackTrace();
    113. }
    114.  
    115. return 0;
    116.  
    117. }
    118.  
    119. synchronized static void addPoints(Player player, int newPoints) {
    120. openConnection();
    121.  
    122. try {
    123.  
    124. PreparedStatement ps1 = connection
    125. .prepareStatement("SELECT points FROM `bukkit`.`user_data` WHERE name=?");
    126. ps1.setString(1, player.getName());
    127. ResultSet result1 = ps1.executeQuery();
    128. result1.next();
    129. int previousPoints = result1.getInt("points");
    130.  
    131. PreparedStatement ps2 = connection
    132. .prepareStatement("UPDATE `user_data` SET `points`=? WHERE name=?");
    133. ps2.setString(2, player.getName());
    134. ps2.setInt(1, newPoints + previousPoints);
    135. ps2.executeUpdate();
    136. ps1.close();
    137. result1.close();
    138. ps2.close();
    139. closeConnection();
    140.  
    141. } catch (Exception e) {
    142. e.printStackTrace();
    143. }
    144.  
    145. }
    146.  
    147. public static String getHability(Player player) {
    148. openConnection();
    149. try {
    150.  
    151. PreparedStatement sql = connection
    152. .prepareStatement("SELECT habilidad FROM `bukkit`.`user_data` WHERE name=? ");
    153. sql.setString(1, player.getName());
    154. ResultSet result = sql.executeQuery();
    155. result.next();
    156. String habilidad = result.getString("habilidad1");
    157. return habilidad;
    158.  
    159. } catch (Exception e) {
    160. e.printStackTrace();
    161. }
    162.  
    163. return null;
    164.  
    165. }
    166.  
    167. }
    168.  


    (I decided to simplify the code so I've put together the old listener class and the main class).

    Thanks for your interest :)
     
  19. idk why :( Everything looks fine to me.
     
Thread Status:
Not open for further replies.

Share This Page