NullPointerException

Discussion in 'Plugin Development' started by confuserr, Apr 29, 2012.

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

    confuserr

    I am getting a java.lang.NullPointerException error, this is the part of the code

    Code:
    package me.confuserr.richlist;
     
    import java.sql.ResultSet;
     
    import java.sql.SQLException;
     
    import java.util.List;
     
    import java.util.logging.Logger;
     
    import me.confuserr.richlist.Database;
     
    import org.bukkit.plugin.PluginDescriptionFile;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    import ru.tehkode.permissions.PermissionManager;
     
    import ru.tehkode.permissions.PermissionUser;
     
    import ru.tehkode.permissions.bukkit.PermissionsEx;
     
    public class RichList extends JavaPlugin {
     
        public final Logger logger = Logger.getLogger("Minecraft");
     
        public static RichList plugin;
     
        private String localUser;
     
        private String localPass;
     
        private String localUrl;
     
        private Database localConn;
     
        private String extUser;
     
        private String extPass;
     
        private String extUrl;
     
        private Database extConn;
     
     
     
     
     
        @Override
     
        public void onDisable() {
     
            PluginDescriptionFile pdfFile = this.getDescription();
     
            this.logger.info(pdfFile.getName() + " has been disabled");
     
        }
     
        @Override
     
        public void onEnable() {
     
            this.getConfig().options().copyDefaults(true);
     
            this.saveConfig();
     
            PluginDescriptionFile pdfFile = this.getDescription();
     
            // Set the database variables from the config
     
            localUser = getConfig().getString("localDatabase.username");
     
            localPass = getConfig().getString("localDatabase.password");
     
            localUrl  = getConfig().getString("localDatabase.url");
     
            extUser = getConfig().getString("externalDatabase.username");
     
            extPass = getConfig().getString("externalDatabase.password");
     
            extUrl  = getConfig().getString("externalDatabase.url");
     
       
     
            this.logger.info(pdfFile.getName() + " Version:" + pdfFile.getVersion() + " has been enabled");
     
            getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
     
                public void run() {
     
                    localConn = new Database(localUser, localPass, localUrl);
     
                    extConn = new Database(extUser, extPass, extUrl);
     
                    extConn.query("TRUNCATE rich_list");
     
                    StringBuffer insertSQL = new StringBuffer();
     
                    insertSQL.append("INSERT INTO "+getConfig().getString("externalDatabase.table")+" (username, balance) VALUES ");
     
                    // Find excluded groups
     
                    StringBuffer notInSQL = new StringBuffer();
     
                    PermissionManager pex = PermissionsEx.getPermissionManager();
     
                    List<String> excludes = getConfig().getStringList("exclude");
     
                    if(!excludes.isEmpty()) {
     
                        notInSQL.append("WHERE username NOT IN(");
     
                        for(String e : excludes) {
     
                            plugin.logger.info(e.toString());
     
                            for(PermissionUser u : pex.getGroup(e.toString()).getUsers()) {
     
                                    notInSQL.append(u.getName()+", ");
     
                            }
     
                        }
     
                        insertSQL.setLength(insertSQL.length() - 2);
     
                        notInSQL.append(")");
     
                    } else
     
                            notInSQL.append("");
     
                    ResultSet result = localConn.query("SELECT username, balance FROM iConomy "+notInSQL.toString()+" ORDER BY balance DESC LIMIT 10");
     
                    try {
     
                        while (result.next()) {
     
                            String username = result.getString("username");
     
                            String balance = result.getString("balance");
     
                            insertSQL.append("('"+username+"', '"+balance+"')");
     
                            insertSQL.append(", ");
     
                        }
     
                        insertSQL.setLength(insertSQL.length() - 2);
     
                        insertSQL.append(";");
     
                        if(!insertSQL.toString().equals("INSERT INTO "+getConfig().getString("externalDatabase.table")+" (username, balance) VALUE;"))
     
                            extConn.query(insertSQL.toString());
     
                        result.close();
     
                        localConn.close();
     
                    } catch (SQLException e) {
     
                        e.printStackTrace();
     
                    }
     
                    extConn.close();
     
                }
     
            }, 60L, 36000L);
     
        }
     
    }
    notInSQL is set to a string buffer. The error is on line 65 which is the plugin.logger.info(e); (there for debugging trying to find out the cause of the error)

    My config file is as follows (database stuff removed for obvious reasons)
    Code:
    localDatabase:
      url:
      username:
      password:
    externalDatabase:
      url:
      username:
      password:
      table:
    exclude:
    - Admin
    - Moderator
    - Owner
    - Co-Owner
    
    Basically i'm trying to find out all the usernames in the groups that are set in exclude from PEX

    Any ideas why its returning null?
     
  2. Offline

    r0306

    Can you post your full class?
     
  3. Offline

    confuserr

    Updated first post with full class
     
  4. Offline

    Njol

    You never initialize 'plugin', thus it is always null which results in a NPE. Add 'plugin = this;' to the beginning of your onEnable.
     
  5. Offline

    r0306

    Or you could try replacing plugin with this.

    Code:
    this.logger.info(e)
     
Thread Status:
Not open for further replies.

Share This Page