Solved value save problem

Discussion in 'Plugin Development' started by Kicksy, Nov 28, 2019.

  1. Offline

    Kicksy

    I want to save a value in a file. But I get only this error always:
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'itemspawn' in plugin BedWars v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_201]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_201]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
    Caused by: java.lang.NullPointerException
            at me.Kicksy.de.itemSpawn.onCommand(itemSpawn.java:26) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more

    My classes:
    Code:
    package me.Kicksy.de;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    public class fileCreate {
    
        public main plugin;
    
        public File itemFile;
        public FileConfiguration itemConfig;
    
        public fileCreate(main main) {
            this.plugin = main;
            itemFile = new File(plugin.getDataFolder() + "/itemSpawn.yml");
            itemConfig = YamlConfiguration.loadConfiguration(itemFile);
    
        }
    
        public void saveCustomYml(FileConfiguration ymlConfig, File ymlFile) {
            try {
                ymlConfig.save(ymlFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    
    Code:
    package me.Kicksy.de;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class itemSpawn implements CommandExecutor {
    
        private main plugin;
        private fileCreate file;
      
      
        public itemSpawn(main main) {
            Bukkit.getPluginCommand("itemspawn").setExecutor((CommandExecutor) this);
        }
      
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (sender instanceof Player) {
                Player p = (Player) sender;
    
                if (cmd.getName().equalsIgnoreCase("itemspawn")) {
                    if(args.length == 2) {
                        if(p.hasPermission("bedwars.itemspawn")) {
                            file.itemConfig.set("spawn.test", 1);               
                            file.saveCustomYml(file.itemConfig, file.itemFile);
                        }
                    }
                }
            }
    
            return true;
    
        }
    
    }
    
    Code:
    package me.Kicksy.de;
    
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin {
    
        private Utilitis util = new Utilitis();
        public fileCreate file;
    
        @Override
        public void onEnable() {
            new itemSpawn(this);
            //getServer().getPluginManager().registerEvents(new events(this), this);
            loadConfiguration();
            file = new fileCreate(this);
            if (!getDataFolder().exists()) {
                getDataFolder().mkdir();
            }
    
            if (!file.itemFile.exists()) {
                try {
                    file.itemFile.createNewFile();
                    file.saveCustomYml(file.itemConfig, file.itemFile);
    
                } catch (Exception e) {
                }
    
            }
    
            System.out.println(util.prefix + "Plugin wurde in der Version:" + util.version + " geladen.");
        }
    
        public void loadConfiguration() {
    
            saveConfig();
        }
    }
    
     
  2. Offline

    CraftCreeper6

    @Kicksy
    By the looks of it. In your stack trace it says:
    Code:
    Caused by: java.lang.NullPointerException
    at me.Kicksy.de.itemSpawn.onCommand(itemSpawn.java:26) ~[?:?]
    Which means that the problem is on line 26 of the itemSpawn class.

    Provided you've given all the code for that class, then when you're running the command you aren't giving any arguments, which would make args.length null.
     
  3. Offline

    KarimAKL

    @Kicksy @CraftCreeper6 I'd guess the stacktrace isn't up to date with the code, and that 'file' is throwing a NullPointerException because it's never initialized.

    I'm pretty sure 'args' is never null, the length is just 0.
     
  4. Offline

    Kicksy

Share This Page