Problem/Bug Getting NullPointerException on createStatement()

Discussion in 'Plugin Help/Development/Requests' started by HeyForms, Dec 23, 2015.

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

    HeyForms

    Hi, good morning!

    I'm developing a BanManager plugin via database MySQL and im stuck on a bug where i get NullPointerException on createStatement().

    Below i'll show the codes and the error!

    ERROR:
    Code:
    Caused by: java.lang.NullPointerException
                 at forms.API.Functions.playerExists(Functions.java:46)
    
    Code from "playerExists()" boolean at Functions.java
    Code:
    public boolean playerExists(Player p) throws SQLException { // Line 45
            Statement st = sql.getConnection().createStatement(); // Line 46
            ResultSet res = st.executeQuery("SELECT * FROM uuid WHERE uuid = '" + p.getUniqueId() + "'"); // Line 47 ...
            if (res.next()) {
                res.close(); st.close();
                return true;
            }
            res.close(); st.close();
            return false;
        }
    
    Where i was wrong?
    Thanks!
     
  2. Online

    timtower Moderator Moderator

    @HeyForms sql could be null, getConnection might return null. Do you know which one of those it could be?
     
  3. Offline

    HeyForms

    @timtower
    Hey,

    Before use "sql" i define him with:
    Code:
    private SQLManager sql = Main.getPlugin().getSQL();
    
    My main class:
    Code:
    private SQLManager sql;
    
    public void onEnable() {
    this.sql = new SQLManager(this, "hostname", "3306", "user", "pass", "dbname");
    }
    
    public SQLManager getSQL() {
            return this.sql;
        }
    
     
  4. Online

    timtower Moderator Moderator

    @HeyForms Seems like it might be getting the sql before the sql is created.
     
  5. Offline

    HeyForms

    Yeah, but, how? The SQL is created on enable. I'm getting crazy :(
     
  6. Online

    timtower Moderator Moderator

    @HeyForms Please show your full main class
     
  7. Offline

    HeyForms

    Ok, lets go.

    Heres my main:

    Code:
    package forms.Bans;
    
    import java.sql.SQLException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import forms.Listeners.PreLogin;
    import forms.Managers.SQLManager;
    
    public class Main extends JavaPlugin {
    
        private SQLManager sql;
        private static Main plugin;
       
        public void onEnable() {
            plugin = this;
           
            getLogger().info("Inicializando");
           
            getLogger().info("Conectando ao MySQL");
           
            // Isolando o SQL
            this.sql = new SQLManager(this, "hostname", "3306", "user", "pass", "dbname");
            // -------------
           
            Bukkit.getPluginManager().registerEvents(new PreLogin(), this);
           
            getLogger().info("Conectado com Sucesso!");
        }
       
        public void onDisable() {
            getLogger().info("Desligando e encerrando conexões!");
            try {
                this.sql.getConnection().close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
       
        public SQLManager getSQL() {
            return this.sql;
        }
       
        public static Main getPlugin() {
            return plugin;
        }
       
    }
    
     
  8. Online

    timtower Moderator Moderator

    @HeyForms Where do you create your functions.java ?
    If that is called before the sql is made or the plugin is even set then you indeed get those errors.
     
  9. Offline

    HeyForms

    Heres my SQLManager where i create the Functions.java:

    Code:
    package forms.Managers;
    
    import java.sql.Connection;
    
    import org.bukkit.plugin.Plugin;
    
    import forms.API.Functions;
    import forms.SQL.MySQL;
    
    public class SQLManager {
    
        private MySQL sql;
        private Connection c;
        private Functions func;
    
        public SQLManager(Plugin plugin, String host, String port, String user, String pass, String db) {
            this.sql = new MySQL(plugin, host, port, db, user, pass);
            this.c = sql.openConnection();
            this.func = new Functions();
        }
    
        public Connection getConnection() {
            return this.c;
        }
       
        public Functions getAPI() {
            return this.func;
        }
       
    
    }
    
    @timtower i use that classes to use MySQL:
    [Tutorial] Using MySQL In your Plugins!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 23, 2015
  10. Online

    timtower Moderator Moderator

    @HeyForms The flow looks fine, so I don't know what is wrong.
     
  11. Offline

    HeyForms

    @timtower me too... And i'm getting crazy with this. But, thanks for you help.
     
Thread Status:
Not open for further replies.

Share This Page