Solved Constructor returning null, why?

Discussion in 'Plugin Development' started by __Sour, Mar 10, 2015.

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

    __Sour

    So I was testing with some stuff today, most better ways to remove some old code, and code it in better and easier ways to understand it. After finishing chat, I moved on the player data, and I think my constructor isn't working as planned.
    http://puu.sh/gvum9/be83cdfb22.png - Chat messages.
    http://puu.sh/gvuom/82bf001825.png - Console log, with error.

    This is the players listener
    Code:
        @EventHandler
        public void join(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            Userdata data = new Userdata(player);
            Chat chat = new Chat(player);
            chat.sendMessage("Loading your " + chat.switcher() + "new" + chat.body() + " stats...");
            data.check(true);
        }
    This is the main class
    Code:
        public void onEnable() {
            setup();
            Bukkit.getServer().getPluginManager().registerEvents(new PlayerListener(), this);
        }
       
        public void setup() {
            new Userdata(this);
            new Chat(this);
        }
    And the player data/userdata class
    Code:
    package srmc.core.user;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    import srmc.core.SRMC;
    import srmc.core.chat.Chat;
    
    public class Userdata {
    
        public SRMC plugin;
        Player user;
        String id;
    
        public Userdata(SRMC core) {
            plugin = core;
        }
       
        public Userdata(Player player) {
            user = player;
            id = player.getUniqueId().toString();
        }
    
        public File dataFolder;
        public File configFile;
        public FileConfiguration config;
    
        public String getCurrentDate() {
            DateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
            Calendar c = Calendar.getInstance();
            return dateFormat.format(c.getTime());
        }
       
        public boolean create() throws IOException {
            dataFolder = new File(plugin.getDataFolder() + File.separator + "Users");
            if (!dataFolder.isDirectory())
                dataFolder.mkdirs();
            configFile = new File(dataFolder, id.toLowerCase() + ".yml");
            config = new YamlConfiguration();
            if (configFile.exists()) {
                configFile.createNewFile();
                config.set("username", user.getName().toString().toLowerCase());
                config.set("user-uuid", id.toLowerCase());
                config.set("socialspy", false);
                config.save(configFile);
            }
            return true;
        }
       
        public boolean save() throws IOException {
            dataFolder = new File(plugin.getDataFolder() + File.separator + "Users");
            if (dataFolder.isDirectory())
                dataFolder.mkdirs();
            configFile = new File(dataFolder, id.toLowerCase() + ".yml");
            try {
                if (configFile.exists())
                    configFile.createNewFile();
                get().save(configFile);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
       
        public FileConfiguration get() {
            dataFolder = new File(plugin.getDataFolder() + File.separator + "Users");
            if (!dataFolder.isDirectory()) {
                return null;
            }
            configFile = new File(dataFolder, id.toLowerCase() + ".yml");
            if (!configFile.exists()) {
                return null;
            }
            FileConfiguration c = YamlConfiguration.loadConfiguration(configFile);
            return c;
        }
    
        public int getUnique() {
            File df = new File(plugin.getDataFolder() + File.separator + "Users");
            return df.listFiles().length;
        }
    
        public boolean exists() {
            return get() != null;
        }
       
        public void check() {
            check(false);
        }
    
        public void check(boolean stat) {
            Chat chat = new Chat(user);
            try {
                if (!exists()) {
                    if (create())
                        chat.sendMessage("Successfully created your " + chat.switcher() + "player data" + chat.body() + ".");
                    else
                        chat.sendMessage("A " + chat.switcher() + "error occured" + chat.body() + " while trying to create your player data.");
                } else {
                    if (stat)
                        chat.sendMessage("Loading your stats...");
                }
            } catch (IOException e1) {
                chat.sendMessage("A " + chat.switcher() + "error occured" + chat.body() + " while trying to create your player data.");
                e1.printStackTrace();
                return;
            }
        }
    
    }
    I don't know why it is returning null, anyone know why?
     
  2. Offline

    teej107

  3. Offline

    guitargun

    @__Sour

    this is your problem

    Code:
        public Userdata(Player player) {
            user = player;
            id = player.getUniqueId().toString();
        }
    because you use it in combination with this
    Code:
            Userdata data = new Userdata(player);
            Chat chat = new Chat(player);
            chat.sendMessage("Loading your " + chat.switcher() + "new" + chat.body() + " stats...");
            data.check(true);

    you are creating a new instance of your userdata. this isn't the same as you do in your main with setup(); this is a new memory reference. plugin is still null so when you do data.check the create you all will throw a null pointer since you ask for the plugin.getDatafolder() which is null
     
Thread Status:
Not open for further replies.

Share This Page