NPE for database connection, but only for one class

Discussion in 'Plugin Development' started by darkhelmet, Jul 17, 2012.

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

    darkhelmet

    I've got a plugin with some database queries and I'm seeing errors like this:

    Code:
    2012-07-17 03:57:52 [SEVERE] Could not pass event PlayerJoinEvent to dhmcStats
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
            at net.minecraft.server.ServerConfigurationManager.c(ServerConfigurationManager.java:134)
            at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:129)
            at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:41)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:66)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:569)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:461)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
            at com.mysql.jdbc.ConnectionImpl.versionMeetsMinimum(ConnectionImpl.java:5488)
            at com.mysql.jdbc.ConnectionImpl.serverSupportsConvertFn(ConnectionImpl.java:4949)
            at com.mysql.jdbc.ConnectionImpl.nativeSQL(ConnectionImpl.java:3981)
            at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4213)
            at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4169)
            at me.botsko.dhmcstats.joins.JoinUtil.registerPlayerJoin(JoinUtil.java:24)
            at me.botsko.dhmcstats.listeners.DhmcstatsPlayerListener.onPlayerJoin(DhmcstatsPlayerListener.java:68)
            at sun.reflect.GeneratedMethodAccessor91.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.java:302)
            ... 10 more

    For code like this:

    Code:
    try {
                if (plugin.conn == null || plugin.conn.isClosed() || !plugin.conn.isValid(1)) plugin.dbc();
                String str = String.format("INSERT INTO joins (username,player_join,ip,player_count) VALUES ('%s','%s','%s','%d')", username, timestamp, ip, online_count );
                PreparedStatement s = plugin.conn.prepareStatement(str);
                s.executeUpdate();
                s.close();
                plugin.conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                Dhmcstats.disablePlugin();
            }

    What I'm not understanding is why it's giving an NPE for the plugin.conn when I specifically do a check for it, and if it's null, we can dbc() again. So many other sections of the plugin use this same method but don't seem to have this problem (that I'm seeing).

    Just in case, here's dbc():

    Code:
    public void dbc(){
           
            String mysql_user = this.getConfig().getString("mysql.username");
            String mysql_pass = this.getConfig().getString("mysql.password");
            String mysql_hostname = this.getConfig().getString("mysql.hostname");
            String mysql_database = this.getConfig().getString("mysql.database");
            String mysql_port = this.getConfig().getString("mysql.port");
           
            java.util.Properties conProperties = new java.util.Properties();
            conProperties.put("user", mysql_user );
            conProperties.put("password", mysql_pass );
            conProperties.put("autoReconnect", "true");
            conProperties.put("maxReconnects", "3");
            String uri = "jdbc:mysql://"+mysql_hostname+":"+mysql_port+"/"+mysql_database;
           
            try {
                conn = DriverManager.getConnection(uri, conProperties);
                if (conn == null || conn.isClosed() || !conn.isValid(1)){
                    this.log("Mysql connection failed to open");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
     
  2. I think you trying to send an null query, I cannot prove it because you dont use debugging statements and dont know what lines your using exact
     
Thread Status:
Not open for further replies.

Share This Page