MySQL Error

Discussion in 'Plugin Development' started by Landen22, Oct 1, 2014.

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

    Landen22

    Have a small Error while I want to set Points in MySQL (first time working with it).

    Code (MySQL) :
    Code:java
    1. package at.landen.db;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.sql.Connection;
    6. import java.sql.DriverManager;
    7. import java.sql.PreparedStatement;
    8. import java.sql.ResultSet;
    9. import java.sql.SQLException;
    10.  
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13.  
    14.  
    15. public class MySQL {
    16.  
    17.  
    18. private String ip;
    19. private String user;
    20. private String pass;
    21. private String db;
    22. private Connection con;
    23.  
    24. public MySQL() throws SQLException{
    25.  
    26. File file = new File("plugins/QuickSurvivalGames/", "database.yml");
    27. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
    28.  
    29. String db = "database.";
    30.  
    31. cfg.addDefault(db+"host", "localhost");
    32. cfg.addDefault(db+"user", "user");
    33. cfg.addDefault(db+"pass", "pw");
    34. cfg.addDefault(db+"datenbank", "db");
    35.  
    36. cfg.options().copyDefaults(true);
    37.  
    38. try {
    39. cfg.save(file);
    40. } catch (IOException e) {
    41. System.err.println("Probleme beim Speichern!");
    42. }
    43.  
    44. this.ip = cfg.getString(db+"host");
    45. this.user = cfg.getString(db+"user");
    46. this.pass = cfg.getString(db+"pass");
    47. this.db = cfg.getString(db+"datenbank");
    48.  
    49. update("CREATE TABLE IF NOT EXISTS QuickSG(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(16), punkte INT)");
    50.  
    51. }
    52.  
    53. public void update(String qr) throws SQLException{
    54. Connection con = this.con;
    55. PreparedStatement st = null;
    56.  
    57.  
    58. st = con.prepareStatement(qr);
    59. st.executeUpdate();
    60.  
    61. }
    62.  
    63.  
    64. Connection con = DriverManager.getConnection("jdbc:mysql://"+this.ip+":3306/"+this.db, this.user, this.pass);
    65.  
    66. this.con = con;
    67. return con;
    68. }
    69.  
    70. public Connection getConnection(){
    71. return this.con;
    72. }
    73.  
    74. public boolean hasConnection() throws SQLException{
    75. return this.con != null || this.con.isValid(1);
    76.  
    77. }
    78.  
    79.  
    80.  
    81. public void closeRess(ResultSet rs, PreparedStatement st) throws SQLException{
    82. if(rs != null){
    83. rs.close();
    84.  
    85. }
    86.  
    87. if(st != null){
    88. st.close();
    89.  
    90. }
    91. }
    92.  
    93. public void closeCon() throws SQLException{
    94. this.con.close();
    95.  
    96. }
    97.  
    98. }
    99.  




    Code (LoginListener):

    Code:java
    1. package at.landen.listener;
    2.  
    3. import java.sql.Connection;
    4. import java.sql.PreparedStatement;
    5. import java.sql.ResultSet;
    6. import java.sql.SQLException;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.GameMode;
    10. import org.bukkit.Location;
    11. import org.bukkit.Material;
    12. import org.bukkit.World;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.potion.PotionEffect;
    19. import org.bukkit.potion.PotionEffectType;
    20.  
    21. import at.landen.db.MySQL;
    22. import at.landen.main.SurvivalGames;
    23.  
    24. public class PlayerJoinListener implements Listener {
    25.  
    26. private SurvivalGames pl;
    27.  
    28. public PlayerJoinListener(SurvivalGames plugin){
    29. this.pl = plugin;
    30.  
    31.  
    32. }
    33.  
    34.  
    35. @EventHandler
    36. public void hasPointsSet(PlayerJoinEvent e) throws SQLException{
    37. givePoint(e.getPlayer().getName(), 50);
    38. }
    39.  
    40. @EventHandler
    41. public void onJoin(PlayerJoinEvent e){
    42. final Player p = e.getPlayer();
    43.  
    44. if(pl.joinable){
    45. for(int i = 0; i != 100; i++ ){
    46. p.sendMessage("");
    47. }
    48.  
    49. e.setJoinMessage(pl.prefix+"Der Spieler §9"+p.getName()+" §6ist dem Spiel beigetretten.");
    50.  
    51. for(Player all : Bukkit.getOnlinePlayers()){
    52. all.showPlayer(all);
    53. }
    54.  
    55. p.setGameMode(GameMode.SURVIVAL);
    56. p.setHealth(20);
    57. p.setFoodLevel(20);
    58. p.setFlying(false);
    59. pl.ingame.add(p);
    60.  
    61. Bukkit.getScheduler().scheduleSyncDelayedTask(pl, new Runnable() {
    62.  
    63. @SuppressWarnings("deprecation")
    64. public void run() {
    65. p.removePotionEffect(PotionEffectType.WEAKNESS);
    66. p.getInventory().clear();
    67. p.getInventory().setArmorContents(null);
    68.  
    69. p.updateInventory();
    70.  
    71. World w = Bukkit.getWorld(pl.getConfig().getString("SurvivalGames.lobby.World"));
    72. Double x = (pl.getConfig().getDouble("SurvivalGames.lobby.x"));
    73. Double y = (pl.getConfig().getDouble("SurvivalGames.lobby.y"));
    74. Double z = (pl.getConfig().getDouble("SurvivalGames.lobby.z"));
    75. p.teleport(new Location(w, x, y, z));
    76.  
    77. }
    78. }, 5);
    79. }else if(!pl.joinable){
    80.  
    81. e.setJoinMessage(null);
    82. p.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 250000, 250000));
    83. p.setHealth(20);
    84. p.getInventory().clear();
    85. p.getInventory().setHelmet(new ItemStack (0, 1));
    86. p.getInventory().setChestplate(new ItemStack (0, 1));
    87. p.getInventory().setLeggings(new ItemStack (0, 1));
    88. p.getInventory().setBoots(new ItemStack (0, 1));
    89. p.setFoodLevel(20);
    90. p.setFireTicks(0);
    91. p.setGameMode(GameMode.CREATIVE);
    92. pl.dead.add(p);
    93. World w = Bukkit.getWorld(pl.getConfig().getString("SurvivalGames.Spec.World"));
    94. Double x = (pl.getConfig().getDouble("SurvivalGames.Spec.x"));
    95. Double y = (pl.getConfig().getDouble("SurvivalGames.Spec.y"));
    96. Double z = (pl.getConfig().getDouble("SurvivalGames.Spec.z"));
    97. p.teleport(new Location(w, x, y, z));
    98. p.getInventory().addItem(new ItemStack(Material.COMPASS));
    99.  
    100.  
    101.  
    102. for(Player all : Bukkit.getOnlinePlayers()){
    103. all.hidePlayer(p);
    104. }
    105. }
    106.  
    107. }
    108.  
    109.  
    110.  
    111.  
    112.  
    113.  
    114.  
    115.  
    116.  
    117.  
    118.  
    119.  
    120.  
    121. public void givePoint(String p, int pu) throws SQLException{
    122. MySQL sql = this.pl.getMySQL();
    123.  
    124. if(punkteSpieler(p) == 0){
    125. sql.update("INSERT INTO QuickSG (name, punkte) VALUES ('"+p+"', '"+pu+"')");
    126. }else{
    127. sql.update("UPDATE QuickSG SET punkte="+punkteSpieler(p)+" WHERE name="+p);
    128. }
    129. }
    130.  
    131. public int punkteSpieler(String player) throws SQLException{
    132. MySQL sql = this.pl.getMySQL();
    133.  
    134. Connection con = sql.getConnection();
    135. ResultSet rs = null;
    136. PreparedStatement st = null;
    137. int punkte = 0;
    138.  
    139. try {
    140. st = con.prepareStatement("SELECT * FROM QuickSG WHERE name=?");
    141. st.setString(1, player);
    142. rs = st.executeQuery();
    143. rs.last();
    144. punkte = rs.getInt("punkte");
    145. } catch (SQLException e) {
    146. e.printStackTrace();
    147. }finally{
    148. sql.closeRess(rs, st);
    149. }
    150. return punkte;
    151. }
    152.  
    153.  
    154.  
    155. }
    156.  




    Error:

    Code:
    16:20:08 [SEVERE] Could not pass event PlayerJoinEvent to QuickSurvivalGames v1.
    8
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at net.minecraft.server.v1_5_R3.PlayerList.c(PlayerList.java:204)
            at net.minecraft.server.v1_5_R3.PlayerList.a(PlayerList.java:100)
            at net.minecraft.server.v1_5_R3.PendingConnection.d(PendingConnection.ja
    va:129)
            at net.minecraft.server.v1_5_R3.PendingConnection.c(PendingConnection.ja
    va:44)
            at net.minecraft.server.v1_5_R3.DedicatedServerConnectionThread.a(Dedica
    tedServerConnectionThread.java:41)
            at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:2
    9)
            at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:5
    81)
            at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:4
    77)
            at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java
    :410)
            at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:5
    73)
    Caused by: java.lang.NullPointerException
            at at.landen.listener.PlayerJoinListener.punkteSpieler(PlayerJoinListene
    r.java:134)
            at at.landen.listener.PlayerJoinListener.givePoint(PlayerJoinListener.ja
    va:124)
            at at.landen.listener.PlayerJoinListener.hasPointsSet(PlayerJoinListener
    .java:37)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 14 more
    >



    Thanks for Help :)
     
  2. Offline

    FabeGabeMC

    Landen22
    Instead of throwing an exception on the void for your event, use try and catch.
    Edit: In German (icu speak german lol) (sorry google translate):
    Stattdessen werfen eine Ausnahme auf die Leere für Ihre Veranstaltung, Einsatz versuchen und zu fangen.
     
  3. Landen22 Assuming stacktrace matches code, your sql variable is null.
     
  4. Offline

    ZachBora

    Like AdamQpzm said, I don't believe you're correctly set your variable that is being sent from
    "this.pl.getMySQL();"
     
  5. Offline

    Nateb1121

    Landen22

    Could you also post your main class that extends JavaPlugin? That way we can gave a look see at that getSQL() method.
     
  6. Offline

    Landen22

    Nateb1121 Here:

    Code:java
    1. public class SurvivalGames extends JavaPlugin {
    2.  
    3. //public PointsAPI points = (PointsAPI) Bukkit.getPluginManager().getPlugin("PointsAPI");
    4.  
    5. private MySQL sql;
    6.  
    7. public String prefix = "§a[§6Quick§a-§bSurvivalGames§a] §6";
    8.  
    9. public ArrayList<Player> dead = new ArrayList<Player>();
    10. public ArrayList<Player> ingame = new ArrayList<Player>();
    11.  
    12. public HashMap<Location, Inventory> sgchest = new HashMap<Location, Inventory>();
    13.  
    14.  
    15. public int exp;
    16. public int expid;
    17.  
    18. public int chestrefi;
    19.  
    20.  
    21. public int matchi;
    22. public int matchid;
    23.  
    24. public int stop;
    25.  
    26. public int stop1;
    27.  
    28. public boolean joinable;
    29. public boolean onspawn;
    30. public boolean friendly;
    31.  
    32. public int start;
    33. public int startid;
    34.  
    35. public void onDisable(){
    36.  
    37.  
    38. }
    39.  
    40. public void onEnable(){
    41.  
    42. Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
    43.  
    44. try{
    45. this.sql = new MySQL();
    46. sql.connect();
    47. }catch(Exception e){
    48.  
    49. }
    50.  
    51.  
    52. for(Player all : Bukkit.getOnlinePlayers()){
    53. all.showPlayer(all);
    54. all.setGameMode(GameMode.SURVIVAL);
    55.  
    56. }
    57.  
    58. SG sg = new SG(this);
    59. getCommand("SurvivalGames").setExecutor(sg);
    60. getCommand("ig").setExecutor(sg);
    61. getCommand("punkte").setExecutor(sg);
    62.  
    63.  
    64.  
    65.  
    66. Bukkit.getPluginManager().registerEvents(new BlockBreakListener(this), this);
    67. Bukkit.getPluginManager().registerEvents(new BlockPlaceListener(), this);
    68. Bukkit.getPluginManager().registerEvents(new EntityDamageListener(this), this);
    69. Bukkit.getPluginManager().registerEvents(new PlayerChangeFoodLevel(this), this);
    70. Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(this), this);
    71. Bukkit.getPluginManager().registerEvents(new PlayerLoginListener(this), this);
    72. Bukkit.getPluginManager().registerEvents(new PlayerMoveListener(this), this);
    73. Bukkit.getPluginManager().registerEvents(new PlayerPickupItemListener(this), this);
    74. Bukkit.getPluginManager().registerEvents(new ChestMananger(this), this);
    75. Bukkit.getPluginManager().registerEvents(new PlayerDeathListener(this), this);
    76. Bukkit.getPluginManager().registerEvents(new PlayerQuitListener(this), this);
    77. Bukkit.getPluginManager().registerEvents(new PlayerDropItemListener(this), this);
    78. Bukkit.getPluginManager().registerEvents(new SpectatorListener(this), this);
    79. Bukkit.getPluginManager().registerEvents(new PlayerPreCommandListener(), this);
    80.  
    81.  
    82.  
    83. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    84.  
    85.  
    86.  
    87. @SuppressWarnings("deprecation")
    88. public void run() {
    89. for(Player p : Bukkit.getOnlinePlayers()){
    90. p.removePotionEffect(PotionEffectType.WEAKNESS);
    91. p.getInventory().clear();
    92. p.getInventory().setArmorContents(null);
    93. p.setHealth(20);
    94. p.setFoodLevel(20);
    95. p.setFireTicks(0);
    96. p.setFlying(false);
    97. p.updateInventory();
    98.  
    99. if(dead.contains(p)){
    100. dead.remove(p);
    101. }
    102. if(ingame.contains(p)){
    103. ingame.remove(p);
    104. ingame.add(p);
    105. }
    106.  
    107. p.showPlayer(p);
    108.  
    109. World w = Bukkit.getWorld(getConfig().getString("SurvivalGames.lobby.World"));
    110. Double x = (getConfig().getDouble("SurvivalGames.lobby.x"));
    111. Double y = (getConfig().getDouble("SurvivalGames.lobby.y"));
    112. Double z = (getConfig().getDouble("SurvivalGames.lobby.z"));
    113. p.teleport(new Location(w, x, y, z));
    114.  
    115. }
    116.  
    117.  
    118. }
    119. },5L);
    120.  
    121.  
    122.  
    123.  
    124.  
    125. this.joinable = true;
    126. this.onspawn = false;
    127.  
    128. if(getConfig().getString("StartZeit") != null){
    129. this.exp = getConfig().getInt("StartZeit");
    130. }else{
    131. int i = 120;
    132. getConfig().set("StartZeit", i);
    133. }
    134.  
    135. this.start = 15;
    136.  
    137. if(getConfig().getString("DeathMatchBeginn") != null){
    138. this.matchi = getConfig().getInt("DeathMatchBeginn");
    139. }else{
    140. int i = 180;
    141. getConfig().set("DeathMatchBeginn", i);
    142. }
    143.  
    144.  
    145. start = 15;
    146.  
    147. stop1 = 0;
    148. mapteleport();
    149. }
    150.  
    151.  
    152.  
    153. public MySQL getMySQL(){
    154. return this.sql;
    155. }


    Not everything from the main but this what should be interesting :D
     
  7. Offline

    fireblast709

    Landen22 You shouldn't be suppressing all the exceptions. It is very likely that an error occurred which you ignored :p.
     
  8. Offline

    Landen22

    fireblast709 what do you meen ? Don´t understand the context with my question :(

    Found the error by myself :D

    removed the sql.connect(); in try{
    this.sql = new MySQL();

    }catch(Exception e){
    System.err.println("Konnt MySQL nicht starten!");
    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page