Solved Error with no obvious reason for me

Discussion in 'Spigot Plugin Development' started by Minimigamer, Jan 15, 2020.

?

Anyone got a solution?

Poll closed Jan 27, 2020.
  1. Obvious Error

    2 vote(s)
    100.0%
  2. Hard to find Error

    0 vote(s)
    0.0%
  1. hey guys i have a problem. again...
    I wrote a plugin with a /home command etc. but there is a NullPointerException in the Main in the line in which i set the commandExecutor:

    so the exception have to be in the home_cmd class so heres the code:

    Code (Text):

    package de.BunchOfSystems.commands;

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;

    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;

    import de.BunchOfSystems.Main.Main;

    public class home implements CommandExecutor {

        ArrayList<String> homes = new ArrayList<String>();

        @Override
        public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("sethome")) {
                if (args.length > 0) {
                    Player p = (Player) s;
                    Location loc;
                    String Filename = p.getName() + ".yml";
                    File file = new File(Main.Path + "/homes/", Filename);

                    FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);

                    if (!(cfg.contains(args[0]))) {
                        loc = new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY() + 0.2, p.getLocation().getZ());
                        cfg.set(args[0], loc);

                        try {
                            cfg.save(file);
                            p.sendMessage(Main.prefix + " §aDein Home wurde gespeichert!");
                        } catch (IOException e) {
                            p.sendMessage(Main.prefix + " §cFehler beim Speichern deines Home-Punktes!");
                            e.printStackTrace();
                        }
                    } else {
                        p.sendMessage(Main.prefix + " §cDieser Home-Punkt existiert bereits!");
                    }
                }
            }

            if (cmd.getName().equalsIgnoreCase("home")) {
                if (args.length > 0) {
                    Player p = (Player) s;

                    String Filename = p.getName() + ".yml";

                    File file = new File(Main.Path + "/homes/", Filename);

                    if (file.exists()) {
                        FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
                        if(cfg.contains(args[0])) {
                            Location home = (Location) cfg.get(args[0]);
                            p.teleport(home);
                        } else {
                            p.sendMessage(Main.prefix + " §cDieser Home-Punkt existiert nicht!");
                        }
                    } else {
                        p.sendMessage(Main.prefix + " §cDu hast noch keinen Home-Punkt festgelegt mache /sethome <HOMENAME> ume ein Home zu setzen!");
                    }
                }
            }

            if (cmd.getName().equalsIgnoreCase("delhome")) {

                if (args.length > 0) {
                    Player p = (Player) s;

                    String Filename = p.getName() + ".yml";

                    File file = new File(Main.Path + "/homes/", Filename);

                    if (file.exists()) {
                        FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);

                        if (cfg.contains(args[0])) {
                            cfg.set(args[0], null);
                            try {
                                cfg.save(file);
                                p.sendMessage(Main.prefix + " §aDein Home wurde gelöscht!");
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else {
                            p.sendMessage(Main.prefix + " §cDieses Home gibt es nicht!");
                        }
                    } else {
                        p.sendMessage(Main.prefix + " §cDu hast noch keinen Home-Punkt festgelegt mache /sethome <HOMENAME> ume ein Home zu setzen!");
                    }
                }
            }
            return false;
        }
    }
    [CODE]
     
  2. Could you post the stacktrace you're getting?
     
  3. You need to show us the line in which the error occurred. If the StackTrace points to the Main.class then show us the Main.class ^^
     
  4. There are more than just this home_cmd class with an error
    the 4 last got errors


    Code (Text):

    package de.BunchOfSystems.Main;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    import de.BunchOfSystems.commands.ChatClear_cmd;
    import de.BunchOfSystems.commands.CmdSpy;
    import de.BunchOfSystems.commands.Day_cmd;
    import de.BunchOfSystems.commands.EnderChest_cmd;
    import de.BunchOfSystems.commands.Feed;
    import de.BunchOfSystems.commands.GM;
    import de.BunchOfSystems.commands.GMA;
    import de.BunchOfSystems.commands.GMC;
    import de.BunchOfSystems.commands.GMS;
    import de.BunchOfSystems.commands.GMSS;
    import de.BunchOfSystems.commands.God_cmd;
    import de.BunchOfSystems.commands.Heal;
    import de.BunchOfSystems.commands.Night_cmd;
    import de.BunchOfSystems.commands.SetSpawn;
    import de.BunchOfSystems.commands.Speed_cmd;
    import de.BunchOfSystems.commands.System;
    import de.BunchOfSystems.commands.Vanish_cmd;
    import de.BunchOfSystems.commands.fly_cmd;
    import de.BunchOfSystems.commands.freeze_cmd;
    import de.BunchOfSystems.commands.home;
    import de.BunchOfSystems.commands.hub_cmds;
    import de.BunchOfSystems.commands.invsee_cmd;
    import de.BunchOfSystems.commands.msg;


    public class Main extends JavaPlugin {
       
    public static String prefix = "§3[§bBunchOfSystems§3]§f ";

    public static String währung = "§c$§a ";
    public static ArrayList<Player> fly = new ArrayList<Player>();
    public static ArrayList<Player> freezed = new ArrayList<Player>();
    public static ArrayList<Player> cmdspy = new ArrayList<Player>();
    public static List<Player> vanished = new ArrayList<Player>();
    public static List<Player> god = new ArrayList<Player>();
    public static String noperm = prefix + "§cDazu hast du keine Rechte!";


    public static String Path = "plugins/BunchOfSystems";



        public void onEnable() {
            File config = new File(Path, "config");
            if(!(config.exists())) {
                FileConfiguration cfg = YamlConfiguration.loadConfiguration(config);
                de.BunchOfSystems.someMethods.SetConfig.main(config, cfg);
            }
           
           
           
           
            Bukkit.getConsoleSender().sendMessage(prefix + " §astarted!");
           
            getCommand("cc").setExecutor(new ChatClear_cmd());
            getCommand("cmdspy").setExecutor(new CmdSpy());
            getCommand("day").setExecutor(new Day_cmd());
            getCommand("feed").setExecutor(new Feed());
            getCommand("fly").setExecutor(new fly_cmd());
            getCommand("freeze").setExecutor(new freeze_cmd());
            getCommand("gm").setExecutor(new GM());
            getCommand("gma").setExecutor(new GMA());
            getCommand("gmc").setExecutor(new GMC());
            getCommand("gms").setExecutor(new GMS());
            getCommand("gmss").setExecutor(new GMSS());
            getCommand("god").setExecutor(new God_cmd());
            getCommand("heal").setExecutor(new Heal());
            getCommand("ec").setExecutor(new EnderChest_cmd());

            getCommand("hub").setExecutor(new hub_cmds());
            getCommand("l").setExecutor(new hub_cmds());
            getCommand("spawn").setExecutor(new hub_cmds());
            getCommand("lobby").setExecutor(new hub_cmds());
            getCommand("ec").setExecutor(new EnderChest_cmd());
            getCommand("invsee").setExecutor(new invsee_cmd());
            getCommand("msg").setExecutor(new msg());
            getCommand("night").setExecutor(new Night_cmd());
            getCommand("setspawn").setExecutor(new SetSpawn());
            getCommand("pay").setExecutor(new System());
            getCommand("money").setExecutor(new System());
            getCommand("seteco").setExecutor(new System());
            getCommand("v").setExecutor(new Vanish_cmd());

            getCommand("speed").setExecutor(new Speed_cmd());      
            getCommand("home").setExecutor(new home());
            getCommand("sethome").setExecutor(new home());
            getCommand("delhome").setExecutor(new home());

        }
    }
     
  5. NullPointerException means that one of your values is null, make sure all of your values are set and be aware of your scopes! (Value visibility global vs local)
     
  6. SteelPhoenix

    Moderator

    See my comments,
    and for the love of god, please follow Java naming convention.

    Edit: Have you made sure you put the commands in plugin.yml

    Code (Java):
    public class home implements CommandExecutor {

        // Why is this field not private?
        // Why is it not defined as a List<String>? Liskov Subsitution Principle is one of the SOLID OOP principles.
        // Why does this field exist here if you don't use it
        ArrayList<String> homes = new ArrayList<String>();

        @Override
        public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("sethome")) {
                if (args.length > 0) {
                    // What is the sender is not a Player?
                    Player p = (Player) s;
                    Location loc;
                    // Use a player's UUID instead of name
                    String Filename = p.getName() + ".yml";
                    File file = new File(Main.Path + "/homes/", Filename);
                    // The file may not exist
                    // Why are you parsing an entire file every time someone executes the command
                    FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);

                    if (!(cfg.contains(args[0]))) {
                        loc = new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY() + 0.2, p.getLocation().getZ());
                        cfg.set(args[0], loc);

                        try {
                            cfg.save(file);
                            // The ChatColor enum exists for a reason
                            p.sendMessage(Main.prefix + " §aDein Home wurde gespeichert!");
                        } catch (IOException e) {
                            // The ChatColor enum exists for a reason
                            p.sendMessage(Main.prefix + " §cFehler beim Speichern deines Home-Punktes!");
                            e.printStackTrace();
                        }
                    } else {
                        // The ChatColor enum exists for a reason
                        p.sendMessage(Main.prefix + " §cDieser Home-Punkt existiert bereits!");
                    }
                }
            }
            // ~ SNIP ~
        }
    }
     
    • Agree Agree x 1
  7. Nice job right there, I would've never had that much patience to comment sll of that class xD
     
  8. We asked for a stacktrace, that's not a stacktrace. I assume it's null because you're not registering your commands in your plugin.yml. Show us that.

    There are other issues in your program too, somebody was nice enough to comment them for you. Make sure to look at that.

    And why did you give us a poll?
     
  9. Thanks that really helped to make my code a little bit better
     
  10. Okay guys im dump af i havent put it in the plugin.yml
    sorry for your work but thanks
     
  11. That's fine, we all make silly mistakes sometimes.
    Just make sure to mark your thread as solved (y)