Always giving null?

Discussion in 'Plugin Development' started by bryce325, Jan 14, 2013.

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

    bryce325

    Code:
        private static EvoTech plugin;
     
     
        public evoSetspawn(EvoTech p) {
            plugin = p;
        }
    I am using this to reference plugin.getConfig() from the main class EvoTech however it always gives a nullpointexception. I have done this in other plugins but have had no problems so I am completely stumped.

    If it helps this method is being called from a commadexectuer class seperate from the main.
     
  2. Offline

    fireblast709

    apparently you are calling the method before initialization, or you have defined another constructor (that does not init plugin) and are using that
     
  3. Offline

    bryce325

    I am honestly lost on what to do to fix it as I ahve tried everything that came to mind.
    The full class is:
    Code:
    package com.aurichan.commands;
     
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
     
    import com.aurichan.EvoTech;
     
    public class evoSetspawn {
        private static EvoTech plugin;
     
     
        public evoSetspawn(EvoTech p) {
            plugin = p;
        }
     
        public static void setSpawn(Player player){
            Location location = player.getLocation();
            String x = location.getBlock().getX()+"";
            String y = location.getBlock().getY()+"";
            String z = location.getBlock().getZ()+"";
            String world = location.getWorld().getName();
            String sLocation = x+","+y+","+z+","+world;
            plugin.getConfig().set("spawn", sLocation);
            plugin.saveConfig();
            player.sendMessage("Spawn is now set.");
            return;
        }
     
    }
    
    Which is getting called from:
    Code:
    package com.aurichan;
     
    import java.io.IOException;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class EvoCmd implements CommandExecutor{
     
        public boolean onCommand(CommandSender sender, Command comd, String label, String[] args){
            Player player = (Player) sender;
            if(comd.getName().equalsIgnoreCase("a")){
                if (args.length == 0){
                    evoHelp.helpCmd(player, "admin");
                    return true;
                }
                String base = args[0];
                if (base.equalsIgnoreCase("setSpawn")){
                    evoSetspawn.setSpawn(player);
                    return true;
                }
    }
    
     
  4. Offline

    stirante

    Better create method like setPlugin(plugin) or init(plugin). Set it to static and then before setSpawn do init(plugin).
     
  5. Offline

    GodzOfMadness

    are you saving your data correctly?
    could i see your main class?
     
  6. Offline

    fireblast709

    bryce325 get rid of the static modifiers and fix the code (without using static again). A tip is to create an instance of evoSetSpawn in the main class and create a getter method to get the object, which you use in the CommandExecutor (which will need a instance of the main class on its turn)
     
  7. Offline

    bryce325

    GodzOfMadness

    Main class:

    Code:
    package com.aurichan;
     
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.logging.Level;
     
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import com.aurichan.commands.evoSetspawn;
    import com.aurichan.listeners.evoJoin;
    import com.aurichan.listeners.evoLogin;
    import com.aurichan.listeners.evoMove;
    import com.aurichan.listeners.evoRespawn;
     
    public final class EvoTech extends JavaPlugin{
     
        public static Map<Player, String> TpRequest = new HashMap<Player, String>();
        public static Map<Player, String> TownInvite = new HashMap<Player, String>();
     
        private FileConfiguration towns = null;
        private File townsFile = null;
        private FileConfiguration players = null;
        private File playersFile = null;
     
        @Override
        public void onEnable(){
            getConfig();
            getPlayers();
            getTowns();
            saveDefaultConfig();
            getCommand("a").setExecutor((CommandExecutor) new EvoCmd());
            getServer().getPluginManager().registerEvents(new evoJoin(this), this);
            getServer().getPluginManager().registerEvents(new evoMove(this), this);
            getServer().getPluginManager().registerEvents(new evoLogin(this), this);
            getServer().getPluginManager().registerEvents(new evoRespawn(this), this);
        }
     
        }
     
    }
    
     
Thread Status:
Not open for further replies.

Share This Page