MySQL Help!

Discussion in 'Plugin Development' started by ColaCraft, Mar 3, 2014.

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

    ColaCraft

    Okay, so. Here is the error I get...

    Code:
    [15:27:15 INFO]: PorterK[/127.0.0.1:33594] logged in with entity id 314 at ([world] 48.2289285790128, 70.0, 251.71919792349917)
    [15:27:15 ERROR]: Could not pass event PlayerJoinEvent to Mortal_Games v0.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.c(PlayerList.java:225) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.a(PlayerList.java:116) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.LoginListener.c(LoginListener.java:78) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.LoginListener.a(LoginListener.java:42) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:149) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
        at me.porterk.mg.MortalAPI.getCash(MortalAPI.java:203) ~[?:?]
        at me.porterk.mg.MortalListener.scoreboard(MortalListener.java:39) ~[?:?]
        at me.porterk.mg.MortalListener.onPlayerLogin(MortalListener.java:60) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_51]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[bukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
        ... 14 more
    
    I have absolutely no idea how MySQL, which is what I have come to conclude it is referring to, could be a NullPointerException.

    Here are my classes:

    Main.java

    Code:java
    1. package me.porterk.mg;
    2.  
    3. import java.io.IOException;
    4. import java.sql.Connection;
    5. import java.sql.SQLException;
    6. import java.sql.Statement;
    7.  
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin{
    16. public boolean SQL;
    17.  
    18. FileConfiguration config = getConfig();
    19.  
    20. MySQL MySQL = new MySQL(this, config.getString("MySQL.host name"),
    21. config.getString("MySQL.port"), config.getString("MySQL.database"),
    22. config.getString("MySQL.username"), config.getString("MySQL.password"));
    23. Connection c = null;
    24.  
    25. Statement statement;
    26. String database = config.getString("MySQL.database");
    27. public String tag = ChatColor.DARK_GRAY + "[" + ChatColor.DARK_RED + "MG" + ChatColor.DARK_GRAY + "]";
    28. public void onEnable(){
    29.  
    30. api().config();
    31.  
    32. SQL = config.getBoolean("MySQL.enabled");
    33.  
    34. config.addDefault("MySQL.username", "user");
    35. config.addDefault("MySQL.password", "pass");
    36. config.addDefault("MySQL.host name", "999.99.99.9");
    37. config.addDefault("MySQL.port", "8080");
    38. config.addDefault("MySQL.database", "mortal_games");
    39. config.addDefault("MySQL.enabled", false);
    40. config.addDefault("Dev Mode", false);
    41. try {
    42. config.save(api().configFile());
    43. } catch (IOException e1) {
    44. e1.printStackTrace();
    45. }
    46.  
    47.  
    48.  
    49. if(SQL){
    50. c = MySQL.openConnection();
    51. }
    52. try{
    53. getServer().getPluginManager().registerEvents(new MortalListener(this), this);
    54. api().debugLog("Listener class registered!");
    55. e.printStackTrace();
    56. api().debugLog("Listener class not registered! (NullPointerException)");
    57. }
    58. api().debugLog("Plugin enabled!");
    59. }
    60.  
    61. public void onDisable(){
    62. api().debugLog("Plugin disabled!");
    63. }
    64.  
    65. public MortalAPI api() {
    66. return new MortalAPI(this);
    67. }
    68.  
    69. public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
    70.  
    71. Player p = (Player) s;
    72.  
    73. if(cmd.getName().equalsIgnoreCase("mg")){
    74.  
    75. if(args[0].equalsIgnoreCase("admin")){
    76.  
    77. api().addToChatChannel(p, api().adminChannel);
    78.  
    79. }
    80.  
    81. if(args[0].equalsIgnoreCase("dev")){
    82.  
    83. if(p.hasPermission("mortal.dev")){
    84.  
    85. if(args[1].equalsIgnoreCase("start")){
    86.  
    87. if(args[2].equalsIgnoreCase("game")){
    88.  
    89. api().startGame();
    90.  
    91. }
    92.  
    93. }else{
    94. p.sendMessage(tag + ChatColor.GOLD + "Not enough arguments.");
    95. }
    96.  
    97. }
    98.  
    99. }
    100.  
    101. }
    102.  
    103. return false;
    104. }
    105. }
    106.  


    MortalAPI.java

    Code:java
    1. package me.porterk.mg;
    2.  
    3. import java.io.File;
    4. import java.io.FileWriter;
    5. import java.io.IOException;
    6. import java.io.PrintWriter;
    7. import java.sql.Connection;
    8. import java.sql.ResultSet;
    9. import java.sql.SQLException;
    10. import java.sql.Statement;
    11. import java.text.SimpleDateFormat;
    12. import java.util.ArrayList;
    13. import java.util.Collections;
    14. import java.util.Date;
    15. import java.util.HashMap;
    16. import java.util.List;
    17.  
    18. import org.bukkit.Bukkit;
    19. import org.bukkit.ChatColor;
    20. import org.bukkit.entity.Player;
    21. import org.bukkit.scoreboard.DisplaySlot;
    22. import org.bukkit.scoreboard.Objective;
    23. import org.bukkit.scoreboard.Score;
    24. import org.bukkit.scoreboard.Scoreboard;
    25. import org.bukkit.scoreboard.ScoreboardManager;
    26. import org.bukkit.scoreboard.Team;
    27.  
    28. public class MortalAPI {
    29.  
    30. Main plugin;
    31.  
    32. public MortalAPI(Main plugin) {
    33. this.plugin = plugin;
    34. }
    35.  
    36. public int beforeStart;
    37. public int preGameTime;
    38. protected boolean isGameOn;
    39. int cash;
    40. int teamNumber;
    41. Statement s = null;
    42. Connection c = null;
    43. String name;
    44. HashMap<Player, Integer> teamSelect = new HashMap<Player, Integer>();
    45. HashMap<Player, String> team = new HashMap<Player, String>();
    46. HashMap<String, Boolean> greenChannel = new HashMap<String, Boolean>();
    47. HashMap<String, Boolean> orangeChannel = new HashMap<String, Boolean>();
    48. HashMap<String, Boolean> spectateChannel = new HashMap<String, Boolean>();
    49. HashMap<String, Boolean> adminChannel = new HashMap<String, Boolean>();
    50.  
    51. public void config(){
    52.  
    53. File config = new File(plugin.getDataFolder() + "/config.yml");
    54. File dir = plugin.getDataFolder();
    55. try{
    56. if(!dir.exists()){
    57. dir.mkdirs();
    58. }
    59. if(!config.exists()){
    60. config.createNewFile();
    61. }
    62.  
    63. }catch(IOException e){
    64. e.printStackTrace();
    65. }
    66.  
    67. }
    68.  
    69. public File configFile(){
    70.  
    71. File config = new File(plugin.getDataFolder() + "/config.yml");
    72.  
    73. return config;
    74. }
    75.  
    76. public void debugLog(String logMessage){
    77.  
    78. if(plugin.config.getBoolean("Dev Mode")){
    79. Date date = new Date();
    80. SimpleDateFormat d1 = new SimpleDateFormat("MM-dd-yyyy");
    81. String t = d1.format(date);
    82. File directory = plugin.getDataFolder();
    83. File logFolder = new File(directory, "/log");
    84. File logFile = new File("plugins/Mortal Games/log/" + t + ".txt");
    85. try{
    86. if(!directory.exists()){
    87. directory.mkdirs();
    88. }
    89. if(!logFolder.exists()){
    90. logFolder.mkdirs();
    91. }
    92. if(!logFile.exists()){
    93. logFile.createNewFile();
    94. }
    95. FileWriter fw = new FileWriter(logFile, true);
    96. PrintWriter pw = new PrintWriter(fw);
    97. SimpleDateFormat d = new SimpleDateFormat("[MM/dd/yyyy HH:mm:ss]");
    98. String t1 = d.format(date);
    99. pw.println(t1 + logMessage);
    100. pw.flush();
    101. pw.close();
    102. }catch(IOException e){
    103. e.printStackTrace();
    104. }
    105. }
    106. }
    107.  
    108. public boolean isGameOn(){
    109.  
    110. return isGameOn;
    111. }
    112.  
    113. public void startGame(){
    114.  
    115. preGameTime = 301;
    116.  
    117. beforeStart = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    118.  
    119. public void run(){
    120.  
    121. preGameTime--;
    122.  
    123. switch(preGameTime){
    124.  
    125. case 300: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "5 minutes");
    126. break;
    127. case 240: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "4 minutes");
    128. break;
    129. case 180: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "3 minutes");
    130. break;
    131. case 120: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "2 minutes");
    132. break;
    133. case 60: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "1 minute");
    134. break;
    135. case 30: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "30 seconds");
    136. break;
    137. case 10: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "10 seconds");
    138. break;
    139. case 9: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "9 seconds");
    140. break;
    141. case 8: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "8 seconds");
    142. break;
    143. case 7: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "7 seconds");
    144. break;
    145. case 6: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "6 seconds");
    146. break;
    147. case 5: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "5 seconds");
    148. break;
    149. case 4: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "4 seconds");
    150. break;
    151. case 3: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "3 seconds");
    152. break;
    153. case 2: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "2 seconds");
    154. break;
    155. case 1: plugin.getServer().broadcastMessage(ChatColor.GOLD + "Game starting in " + ChatColor.DARK_RED + "1 seconds");
    156. break;
    157. case 0: plugin.getServer().broadcastMessage(ChatColor.GREEN + "Prepare yourself," + ChatColor.DARK_RED + " mortal " +
    158. ChatColor.GREEN + ", the game is starting...");
    159.  
    160. debugLog("Game started");
    161.  
    162. return;
    163. }
    164.  
    165. }
    166.  
    167. }, 0L, 20L);
    168.  
    169. }
    170.  
    171. public void gameStart(){
    172.  
    173. List<Player> players = new ArrayList<Player>();
    174. for (Player player : Bukkit.getOnlinePlayers()) {
    175. players.add(player);
    176. }
    177.  
    178. Collections.shuffle(players);
    179.  
    180. teamNumber = 0;
    181. for (Player player : players) {
    182. teamSelect.put(player, teamNumber++);
    183. if (teamNumber >= 6) {
    184. team.put(player, "orange");
    185. orangeChannel.put(player.getName(), true);
    186. }else{
    187. team.put(player, "green");
    188. greenChannel.put(player.getName(), true);
    189. }
    190. }
    191.  
    192. }
    193. public String getTeam(Player p){
    194.  
    195. return team.get(p);
    196. }
    197.  
    198. public int getCash(Player p){
    199.  
    200. try{
    201.  
    202. name = p.getName();
    203. Statement cs = plugin.MySQL.openConnection().createStatement(); //THE ERROR IS HERE
    204. ResultSet res = cs.executeQuery("SELECT * FROM mortal WHERE PlayerName = '" + name.toString() + "';");
    205.  
    206. if(!res.next()){
    207. plugin.statement.executeUpdate("INSERT INTO mortal (`PlayerName`, `inGameCash`, `totalCash`, " +
    208. "`totalKills`, `serverCash`, `rank`) VALUES ('" + p.getName() + "', '500', '500', '0', '0', 'Default');");
    209. }else{
    210. res.next();
    211. }
    212.  
    213. if(res.getString("PlayerName") == null){
    214.  
    215. cash = 1;
    216.  
    217. }else {
    218.  
    219. cash = res.getInt("inGameCash");
    220. }
    221. } catch (SQLException e) {
    222. e.printStackTrace();
    223. }
    224. return cash;
    225. }
    226.  
    227.  
    228. public void addToChatChannel(Player p, HashMap<String, Boolean> channel){
    229.  
    230. if(channel != null){
    231.  
    232. if(!channel.containsKey(p.getName())){
    233. channel.put(p.getName(), true);
    234. }else{
    235. channel.put(p.getName(), false);
    236. }
    237.  
    238. }
    239. }
    240.  
    241. }
    242.  


    MortalListener.java

    Code:java
    1. package me.porterk.mg;
    2.  
    3.  
    4. import java.sql.SQLException;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.AsyncPlayerChatEvent;
    12. import org.bukkit.event.player.PlayerJoinEvent;
    13. import org.bukkit.scoreboard.DisplaySlot;
    14. import org.bukkit.scoreboard.Objective;
    15. import org.bukkit.scoreboard.Score;
    16. import org.bukkit.scoreboard.Scoreboard;
    17. import org.bukkit.scoreboard.ScoreboardManager;
    18.  
    19. public class MortalListener implements Listener{
    20.  
    21. Main plugin;
    22.  
    23. public MortalListener(Main plugin) {
    24. this.plugin = plugin;
    25. }
    26.  
    27.  
    28. public MortalAPI api() {
    29. return new MortalAPI(null);
    30. }
    31.  
    32. public void scoreboard(Player p) {
    33. Objective obj = p.getScoreboard().registerNewObjective(p.getName() + "_score", "dummy");
    34.  
    35. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    36. obj.setDisplayName(ChatColor.DARK_RED + "The Mortal Games");
    37.  
    38. Score cash = obj.getScore((Bukkit.getOfflinePlayer(ChatColor.GREEN + "Cash:")));
    39. cash.setScore(api().getCash(p));
    40.  
    41. p.setScoreboard(Bukkit.getServer().getScoreboardManager().getNewScoreboard());
    42. }
    43.  
    44.  
    45.  
    46. @EventHandler
    47. public void onPlayerLogin(PlayerJoinEvent e){
    48.  
    49. e.setJoinMessage(null);
    50.  
    51. Player p = e.getPlayer();
    52. int playerCount = p.getServer().getOnlinePlayers().length;
    53.  
    54. if(playerCount < 10){
    55. p.sendMessage(ChatColor.GOLD + "Welcome, " + ChatColor.GREEN + p.getDisplayName() +
    56. ChatColor.GOLD + " to " + ChatColor.DARK_RED + "The Mortal Games");
    57. p.sendMessage(ChatColor.BLUE + "Visit our website at" + ChatColor.RED + " [URL]http://gld.bz[/URL]!");
    58. p.sendMessage(ChatColor.GOLD + "There are currently " + ChatColor.DARK_RED + playerCount
    59. + "/10" + ChatColor.GOLD +" players online! :)");
    60. this.scoreboard(p);
    61. p.sendMessage("Scoreboard added");
    62. }else if(playerCount >= 10){
    63. p.sendMessage(ChatColor.GOLD + "Welcome to " + ChatColor.DARK_RED + "The Mortal Games");
    64. p.sendMessage(ChatColor.BLUE + "Visit our website at" + ChatColor.RED + " [URL]http://gld.bz[/URL]!");
    65. p.sendMessage(ChatColor.DARK_RED + "" +ChatColor.ITALIC + "Currently the game is full.");
    66. }
    67.  
    68. if(p.isOp()){
    69. p.sendMessage(ChatColor.AQUA + "Type /admin to get to admin chat!");
    70. }
    71.  
    72. if(playerCount == 10){
    73. api().startGame();
    74. }
    75.  
    76.  
    77. }
    78.  
    79. @EventHandler
    80. public void onPlayerChat(AsyncPlayerChatEvent e){
    81.  
    82.  
    83.  
    84. }
    85.  
    86. }
    87.  


    Thanks for any help. The SQL library I am using is Husky's.

    Super epic bump of helpingness

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

    GaaTavares

    Is your plugin variable null?
     
  3. Offline

    ColaCraft

    It doesn't seem to be. My other Methods work.

    For whatever reason anything that calls upon that MySQL object throws me a NullPointerException

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

    ColaCraft

    Bump

    DevRosemberg I saw you were online- thought you could help
     
Thread Status:
Not open for further replies.

Share This Page