Plugin not writing to custom .yml file

Discussion in 'Spigot Plugin Development' started by Videosambo, Sep 17, 2018.

  1. Hello, I got error when I try to write data to custom config file.
    Error log:
    https://pastebin.com/XhLvvqAk

    Code:
    Code (Text):

    public class CreatePortal {

        public void makePortal(int x1, int y1, int z1, int x2, int y2, int z2, String s, String world) {
           
            int xu1 = x1;
            int yu1 = y1;
            int zu1 = z1;
            int xu2 = x2;
            int yu2 = y2;
            int zu2 = z2;
           
            new DebugCommand();
            boolean dc = DebugCommand.debug;
            Main plugin = Main.getPlugin(Main.class);
            ConfigurationManager config = new ConfigurationManager();
            String pm = plugin.getConfig().getString("portalmaterial");

            switch (s) {

            case "x":
                x1 = x1 + 1;
                y1 = y1 + 1;
                x2 = x2 - 1;
                y2 = y2 - 1;
                for (int x = x1; x <= x2; x++) {
                    for (int y = y1; y <= y2; y++) {
                        Bukkit.getServer().getWorld(world).getBlockAt(x, y, z1).setType(Material.getMaterial(pm));
                    }
                }
               
                config.getPortals().set(world+"."+xu1+"_"+yu1+"_"+zu1+"__"+zu2+"_"+yu2+"_"+zu2+".corner1.x", xu1);
                config.getPortals().set(world+"."+xu1+"_"+yu1+"_"+zu1+"__"+zu2+"_"+yu2+"_"+zu2+".corner1.y", yu1);
                config.getPortals().set(world+"."+xu1+"_"+yu1+"_"+zu1+"__"+zu2+"_"+yu2+"_"+zu2+".corner1.z", zu1);

                config.getPortals().set(world+"."+xu1+"_"+yu1+"_"+zu1+"__"+zu2+"_"+yu2+"_"+zu2+".corner2.x", xu2);
                config.getPortals().set(world+"."+xu1+"_"+yu1+"_"+zu1+"__"+zu2+"_"+yu2+"_"+zu2+".corner2.y", yu2);
                config.getPortals().set(world+"."+xu1+"_"+yu1+"_"+zu1+"__"+zu2+"_"+yu2+"_"+zu2+".corner2.z", zu2);
               
                config.savePortals();
               
                break;

                                   //here is more code...
           }  
        }
    }
     
     
  2. Show us your ConfigurationManager class
     
  3. Code (Text):

    package com.videosambo.motimaa.Taivas;

    import java.awt.TrayIcon.MessageType;
    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    public class ConfigurationManager {

        private Main plugin = Main.getPlugin(Main.class);
        public FileConfiguration portals;
        public File portalsfile;

        public void setup() {
            if (!plugin.getDataFolder().exists()) {
                plugin.getDataFolder().mkdir();
            }

            portalsfile = new File(plugin.getDataFolder(), "portals.yml");

            if (!portalsfile.exists()) {
                try {
                    portalsfile.createNewFile();
                    Bukkit.getServer().getConsoleSender().sendMessage("portals.yml has been created");
                } catch (IOException e) {
                    Bukkit.getServer().getConsoleSender().sendMessage(MessageType.ERROR + "Could not create portals.yml");
                }
            }
           
            portals = YamlConfiguration.loadConfiguration(portalsfile);
        }
       
        public FileConfiguration getPortals() {
            return portals;
        }
       
        public void savePortals() {
            try {
                portals.save(portalsfile);
            } catch (IOException e){
                Bukkit.getServer().getConsoleSender().sendMessage(MessageType.ERROR + "Could not save portals.yml");
            }
        }
       
        public void reloadPortals() {
            portals = YamlConfiguration.loadConfiguration(portalsfile);
        }

    }
     
     
  4. 7GV

    7GV

    That nullptr error seems to be pointing to line 44<, which is not covered on the given source.
    May you post the rest of that class to give more information about the problem.
     
  5. Sorry, I removed couple empty rows, i'ts should be
    Code (Text):

    config.getPortals().set(world+"."+xu1+"_"+yu1+"_"+zu1+"__"+zu2+"_"+yu2+"_"+zu2+".corner2.x", xu2);
     
     
  6. Choco

    Moderator

    The result of #getPortals() is the only thing that would throw an NPE here

    Which seems to add up given the above snippets. Your "portals" field in the ConfigurationManager is only instantiated when ConfigurationManager#setup() is invoked which it never is. This is why we have constructors.
     
    • Like Like x 1