Error!

Discussion in 'Spigot Plugin Development' started by Deger, Jun 7, 2016.

  1. Hi i got an error:
    Check the error tab!!

    This is line 17 on warp plugin:
    settings.createFiles();
    This is the object i made
    SettingsManager settings = new SettingsManager();

    This is line 25 on settingsmanager
    dfile = new File(pl.getDataFolder(), "data.yml");

    Code:
    Code (Text):
    package me.deger.warp;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;


    public class SettingsManager {
     

        Plugin pl;
        File dfile;
        FileConfiguration data;
     
     
        public FileConfiguration getData(){
            return this.data;
        }
     
        public void createFiles(){
            dfile = new File(pl.getDataFolder(), "data.yml");
         
            if(!dfile.exists()){
                dfile.getParentFile().mkdirs();
                pl.saveResource("data.yml", false);
            }
         
            data = new YamlConfiguration();
            try {
                data.load(dfile);
            } catch (IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
        }

        public void saveData() {
            try {
                data.save(dfile);
            } catch (IOException e) {
                e.printStackTrace();
            }
         
        }

    }
     

    Warp plugin:
    Code (Text):
    package me.deger.warp;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Warp extends JavaPlugin {
     
        SettingsManager settings = new SettingsManager();

        public void onEnable() {
         settings.createFiles();
        }

        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You need to be a player to use this command!");
                return true;
            }
            Player p = (Player) sender;
            if (label.equalsIgnoreCase("tp")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a player");
                    return true;
                }
                if (args.length > 1) {
                    p.sendMessage(ChatColor.RED + "Usage: /tp <playername>");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    p.sendMessage(ChatColor.RED + "Could not find player " + ChatColor.WHITE + "\"" + ChatColor.GREEN
                            + args[0] + ChatColor.WHITE + "\"");
                    return true;
                }
                p.teleport(target.getLocation());
                return true;
            }

            if (label.equalsIgnoreCase("setspawn")) {
                 settings.getData().set("spawn.world", p.getLocation().getWorld().getName());
                settings.getData().set("spawn.x", p.getLocation().getX());
                settings.getData().set("spawn.y", p.getLocation().getY());
                settings.getData().set("spawn.z", p.getLocation().getZ());
                settings.getData().set("spawn.yaw", p.getLocation().getYaw());
                settings.getData().set("spawn.pitch", p.getLocation().getPitch());
                settings.saveData();

                p.sendMessage(ChatColor.GREEN + "Spawn set!");
                return true;
            }
                if (label.equalsIgnoreCase("spawn")) {
                    if (settings.getData().getConfigurationSection("spawn") == null) {
                        p.sendMessage(ChatColor.RED + "The spawn has not yet been set!");
                        return true;
                    }
                    World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn.world"));
                    double x = settings.getData().getDouble("spawn.x");
                    double y = settings.getData().getDouble("spawn.y");
                    double z = settings.getData().getDouble("spawn.z");
                    float yaw = (float) settings.getData().getDouble("spawn.yaw");
                    float pitch = (float) settings.getData().getDouble("spawn.pitch");
                    p.teleport(new Location(w, x, y, z, yaw, pitch));
                    return true;
                }

            if (label.equalsIgnoreCase("delspawn")) {
                settings.getData().set("spawn", null);
                settings.saveData();
                return true;
            }

            if (label.equalsIgnoreCase("setwarp")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a name!");
                    p.sendMessage(ChatColor.RED + "Usage: /setwarp <warpname>");
                    return true;
                }
                settings.getData().set("warp." + args[0] + ".world", p.getLocation().getWorld().getName());
                settings.getData().set("warp." + args[0] + ".x", p.getLocation().getX());
                settings.getData().set("warp." + args[0] + ".y", p.getLocation().getY());
                settings.getData().set("warp." + args[0] + ".z", p.getLocation().getZ());
                settings.getData().set("warp." + args[0] + ".pitch", p.getLocation().getPitch());
                settings.saveData();
                p.sendMessage(ChatColor.GREEN + "Set warp " + args[0] + "!");
                return true;
            }
            if (label.equalsIgnoreCase("warp")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a name!");
                    p.sendMessage(ChatColor.RED + "Usage: /warp <warpname>");
                    return true;
                }

                if (settings.getData().getConfigurationSection("warps. " + args[0]) == null) {
                    p.sendMessage(ChatColor.RED + "Warp " + ChatColor.GRAY + args[0] + ChatColor.RED + " does not exist!");
                    return true;
                }
                // if (settings.getData().getConfigurationSection("warps." + args[0]) ==
                // null){
                // p.sendMessage(ChatColor.RED + "Warp " + args[0] + " does not
                // exist!");
                // }
                World w = Bukkit.getServer().getWorld(settings.getData().getString("warp." + args[0] + ".world"));
                double x = settings.getData().getDouble("warp." + args[0] + ".x");
                double y = settings.getData().getDouble("warp." + args[0] + ".y");
                double z = settings.getData().getDouble("warp." + args[0] + ".z");
                float yaw = (float) settings.getData().getDouble("warp." + args[0] + ".yaw");
                float pitch = (float) settings.getData().getDouble("warp." + args[0] + ".pitch");
                p.teleport(new Location(w, x, y, z, yaw, pitch));
                p.sendMessage(ChatColor.GREEN + "Teleported to warp " + args[0] + "!");
                return true;
            }

            if (label.equalsIgnoreCase("delwarp")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a name!");
                    p.sendMessage(ChatColor.RED + "Usage: /delwarp <warpname>");
                    return true;
                }
                if (settings.getData().getConfigurationSection("warps." + args[0]) == null)
                    ;
                {
                    p.sendMessage(ChatColor.RED + "Warp " + args[0] + " does not exist!");
                }
                settings.getData().set("warps." + args[0], null);
                settings.saveData();
                p.sendMessage(ChatColor.GREEN + "Removed warp " + args[0]);
                return true;

            }
            return false;
        }
    }
     


    Error:
    Code (Text):
    [22:46:04 ERROR]: Error occurred while enabling Teleporteeer v1.0 (Is it up to date?)
    java.lang.NullPointerException
            at me.deger.warp.SettingsManager.createFiles(SettingsManager.java:27) ~[?:?]
            at me.deger.warp.Warp.onEnable(Warp.java:17) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.loadPlugin(CraftServer.java:361) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.enablePlugins(CraftServer.java:321) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.reload(CraftServer.java:745) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.Bukkit.reload(Bukkit.java:539) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:645) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchServerCommand(CraftServer.java:631) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.DedicatedServer.aL(DedicatedServer.java:437) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
     
  2. Your "pl" variable is probably null.
     
  3. Yes ánd how should i set it?
     
  4. Please do yourself a favor and start by learning Java. It's like your fifth post today alone and this error is an NPE.
     
    • Agree Agree x 1
  5. Make a constructor for your SettingsManager class that takes an instance of your main class.
     
  6. Finally i could ignore!! Ferdz
     
  7. What is line 27 in SettingsManager