SQL Help

Discussion in 'Plugin Development' started by flash1110, Jul 29, 2017.

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

    flash1110

    I keep on getting this error on PlayerLogin:
    [10:12:57 WARN]: java.lang.NullPointerException
    [10:12:57 WARN]: at me.flash1110.essentials.SQL.LoadPlayer.run(LoadPlayer.java:51)
    [10:12:57 WARN]: at java.lang.Thread.run(Thread.java:748)

    The error is from this line:
    Code:
    CurrentStats = st.executeQuery("SELECT * FROM EMPIREWARS_PLAYER_STATS WHERE UUID = '" + playerid.toString() + "';");
    From the method:
    Code:
    @Override
        public void run() {
            Connection con = null;
            Statement st = null;
    
            String host = plugin.getConfig().getString("MySQL.HostName");
            String port = plugin.getConfig().getString("MySQL.Port");
            String db = plugin.getConfig().getString("MySQL.DataBaseName");
            String user = plugin.getConfig().getString("MySQL.UserName");
            String password = plugin.getConfig().getString("MySQL.Password");
           
            String url = "jdbc:mysql://" + host + ":" + port + "/" + db;
           
            try {
                con = DriverManager.getConnection(url, user, password);
                st = con.createStatement();
               
                Boolean Exists = false;
                ResultSet CurrentStats = null;
               
                try {
                    CurrentStats = st.executeQuery("SELECT * FROM EMPIREWARS_PLAYER_STATS WHERE UUID = '" + playerid.toString() + "';");
                   
                    if (CurrentStats.first())
                        Exists = true;
                   
                    if (Exists) {
                        // 0:0:0:0 = Player Kills
                        // 0:0:0:0:0 = Deaths
                           
                        // Format = <ID>=[amount]:<ID>=[amount]
                       
                        Integer Stats[] = new Integer[Main.StatArrayListSize];
                       
                        for (int i=0; i<Stats.length; i++)
                            Stats[i] = 0;
                       
                        Stats[0] = CurrentStats.getInt("Online_Time");
                       
                        String[] Player_Kills = CurrentStats.getString("Player_Kills").split(":");
                       
                        for (String S : Player_Kills) {
                           
                            String[] Split = S.split("=");
                           
                            Integer ID = Integer.parseInt(Split[0]);
                            Integer Amount = Integer.parseInt(Split[1]);
                           
                            Stats[ID] = Amount;
                           
                        }
                       
                        String[] Player_Deaths = CurrentStats.getString("Deaths").split(":");
                       
                        for (String S : Player_Deaths) {
                            String[] Split = S.split("=");
                           
                            Integer ID = Integer.parseInt(Split[0]);
                            Integer Amount = Integer.parseInt(Split[1]);
                           
                            Stats[ID] = Amount;
                        }
                       
                        Long FirstJoin = CurrentStats.getLong("Join_Date");
                       
                        if (Main.containsStatPlayer(playerid))
                            Main.getStatPlayer(playerid).loadTotal(Stats, FirstJoin);
                       
                        plugin.LogMessage(Level.INFO, "Loaded Stats sucessfully for " + playername);
                    } else {
                        String Player_Kills = "1=0:2=0:3=0:4=0";
                        String Player_Deaths = "6=0:7=0:8=0:9=0:10=0";
                       
                        st.execute("INSERT INTO EMPIREWARS_PLAYER_STATS(ID, UUID, Player_Name, Player_Name_Low, Online_Time, Join_Date, Player_Kills, Deaths) "
                                + "VALUES (NULL, '" + playerid.toString() + "', '" + playername + "', '" + playername.toLowerCase() + "', '0', "
                                + "'" + System.currentTimeMillis() + "', "
                                + "'" + Player_Kills + "', "
                                + "'" + Player_Deaths + "');");
                       
                        plugin.LogMessage(Level.INFO, "Added user " + playername + " to stats database");
                       
                        Integer Stats[] = new Integer[Main.StatArrayListSize];
                       
                        for (int i=0; i<Stats.length; i++)
                            Stats[i] = 0;
                       
                        if (Main.containsStatPlayer(playerid))
                            Main.getStatPlayer(playerid).loadTotal(Stats, System.currentTimeMillis());
                    }
                } catch (SQLException e) {
                    plugin.LogMessage(Level.WARNING, "MySQL Query Failed. Try again later");
                    e.printStackTrace();
                }
            } catch (SQLException ex) {
                plugin.LogMessage(Level.WARNING, "MySQL Failed to connect. Please check login details");
                ex.printStackTrace();
            } finally {
                try {
                    if (st != null) {
                        st.close();
                    }
                   
                    if (con != null) {
                        con.close();
                    }
                } catch (SQLException ex) {
                    plugin.LogMessage(Level.WARNING, "MySQL Failed to connect. Please check login details");
                    ex.printStackTrace();
                }
            }
        }
    I have no idea why the error is happening? I used the same connection/statement method in another class which worked - it created the table.
     
  2. Offline

    Caderape2

    @flash1110 something is null on line 51. Probably a field that is not initialized.
     
  3. Offline

    Zombie_Striker

    @flash1110
    St or playerid is most likely null. Try printing those values to see which one is null. If 'st' is null, it may be that that the connection is not open. If 'playerid' is null, you need to set it.
     
Thread Status:
Not open for further replies.

Share This Page