Solved NPE trying to load holograms

Discussion in 'Plugin Development' started by Blackwing_Forged, Sep 2, 2017.

Thread Status:
Not open for further replies.
  1. Its giving me a npe on this line

    Code:
    private void loadHolo(Location loc, String name)
        {
            Hologram hologram = HologramsAPI.createHologram(CrateMain.getInstance(), loc);
            hologram.appendTextLine(ChatColor.translateAlternateColorCodes('&', name)); //error here
        }
    and this is how i use the method

    Code:
    for(File file : getDataFolder().listFiles())
            {
                String fileName = file.getName().replaceAll(".yml", "");
               
                if(CrateCommands.crateConfig.contains("Holo." + fileName))
                {
                    System.out.println("CONTAINS FILE");
                   
                    for(String key : CrateCommands.crateConfig.getConfigurationSection("Holo").getKeys(false))
                    {
                        String world = CrateCommands.crateConfig.getString("Holo." + fileName + ".World");
                        double x = CrateCommands.crateConfig.getDouble("Holo." + key + ".x");
                        double y = CrateCommands.crateConfig.getDouble("Holo." + key + ".y");
                        double z = CrateCommands.crateConfig.getDouble("Holo." + key + ".z");
                        String name = CrateCommands.crateConfig.getString("Holo." + key + ".Name");
                       
                        Location loc = new Location(Bukkit.getWorld(world), x, y, z);
                        if(world != null && name != null)
                        {
                            loadHolo(loc, name);
                        }
                    }
                }
            }
    and this is the error
    Error (open)

    02.09 13:17:30 [Server] INFO at net.ascendmc.CrateMain.onEnable(CrateMain.java:59) ~[?:?] 02.09 13:17:30 [Server] INFO at net.ascendmc.CrateMain.loadHolo(CrateMain.java:80) ~[?:?] 02.09 13:17:30 [Server] INFO at com.gmail.filoghost.holographicdisplays.object.CraftHologram.appendTextLine(CraftHologram.java:1) ~[?:?] 02.09 13:17:30 [Server] INFO at com.gmail.filoghost.holographicdisplays.object.CraftHologram.appendTextLine(CraftHologram.java:120) ~[?:?] 02.09 13:17:30 [Server] INFO at com.gmail.filoghost.holographicdisplays.object.CraftHologram.refreshSingleLines(CraftHologram.java:270) ~[?:?] 02.09 13:17:30 [Server] INFO at com.gmail.filoghost.holographicdisplays.object.line.CraftTextLine.spawn(CraftTextLine.java:74) ~[?:?] 02.09 13:17:30 [Server] INFO at com.gmail.filoghost.holographicdisplays.util.MinecraftVersion.isGreaterEqualThan(MinecraftVersion.java:50) ~[?:?] 02.09 13:17:30 [Server] INFO java.lang.NullPointerException
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Code:
    public class CrateMain extends JavaPlugin
    {
        private static CrateMain instance;
    
        public CrateMain()
        {
            instance = this;
        }
    
        public static CrateMain getInstance()
        {
            return instance;
        }
       
        public void onEnable()
        {
            if(!getDataFolder().exists())
                getDataFolder().mkdir();
           
            this.saveConfig();
            registerCommands();
            registerEvents();
           
            for(File file : getDataFolder().listFiles())
            {
                String fileName = file.getName().replaceAll(".yml", "");
               
                if(CrateCommands.crateConfig.contains("Holo." + fileName))
                {
                    System.out.println("CONTAINS FILE");
                   
                    for(String key : CrateCommands.crateConfig.getConfigurationSection("Holo").getKeys(false))
                    {
                        String world = CrateCommands.crateConfig.getString("Holo." + fileName + ".World");
                        double x = CrateCommands.crateConfig.getDouble("Holo." + key + ".x");
                        double y = CrateCommands.crateConfig.getDouble("Holo." + key + ".y");
                        double z = CrateCommands.crateConfig.getDouble("Holo." + key + ".z");
                        String name = CrateCommands.crateConfig.getString("Holo." + key + ".Name");
                       
                        Location loc = new Location(Bukkit.getWorld(world), x, y, z);
                        if(world != null && name != null)
                        {
                            loadHolo(loc, name);
                        }
                    }
                }
            }
        }
    
        private void registerEvents()
        {
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(new CrateOpenEvent(), this);
        }
       
        private void registerCommands()
        {
            getCommand("crate").setExecutor(new CrateCommands());
        }
       
        public static void loadHolo(Location loc, String name)
        {
            Hologram hologram = HologramsAPI.createHologram(CrateMain.getInstance(), loc);
            hologram.appendTextLine(ChatColor.translateAlternateColorCodes('&', name));
        }
    }
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. @timtower
    whats the best way to get my plugin instance then
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Blackwing_Forged Constructors, just pass them along to child classes.
    new CrateCommands(this) for example.
    And stay away from public static, you don't need it.
     
  7. now im getting an error on this line:
    Code:
    CrateMain instance;
       
        public CrateCommands(CrateMain instance)
        {
            instance = this.instance;
        }
       
        public File crateFile = new File(instance.getDataFolder() + "/crates.yml"); //error here
        public FileConfiguration crateConfig = YamlConfiguration.loadConfiguration(crateFile);
     
  8. Offline

    Caderape2

    @Blackwing_Forged
    this is a keyword for refer to a field outside of your method.
    this.instance = instance will work.
     
  9. @Caderape2
    Im still getting an error on this line though
    public File crateFile = new File(instance.getDataFolder() + "/crates.yml");
     
  10. Offline

    Caderape2

  11. @Caderape2
    Ok now im getting this error
    03.09 11:48:43 [Server] INFO java.lang.NullPointerException: source's world cannot be null

    on this line:
    Code:
    private void loadHolo(Location loc, String name)
        {
            Hologram hologram = HologramsAPI.createHologram(this, loc); //error
            hologram.appendTextLine(ChatColor.translateAlternateColorCodes('&', name));
        }
    and this is how im using it

    Code:
    for(File file : getDataFolder().listFiles())
            {
                String fileName = file.getName().replaceAll(".yml", "");
              
                if(CrateCommands.crateConfig.contains("Holo." + fileName))
                {
                    System.out.println("CONTAINS FILE");
                  
                    for(String key : CrateCommands.crateConfig.getConfigurationSection("Holo").getKeys(false))
                    {
                        String world = CrateCommands.crateConfig.getString("Holo." + fileName + ".World");
                        double x = CrateCommands.crateConfig.getDouble("Holo." + key + ".x");
                        double y = CrateCommands.crateConfig.getDouble("Holo." + key + ".y");
                        double z = CrateCommands.crateConfig.getDouble("Holo." + key + ".z");
                        String name = CrateCommands.crateConfig.getString("Holo." + key + ".Name");
                      
                        Location loc = new Location(Bukkit.getWorld(world), x, y, z);
                        if(world != null && name != null)
                        {
                            loadHolo(loc, name);
                        }
                    }
                }
            }
     
  12. Offline

    LRK

    @Blackwing_Forged Can you please post your config ?
    Seems like bukkit cant find the world. Are you sure that the world exists?
     
  13. yeah im sure
    Heres what the config looks like
    Code:
    Holo:
      Templar:
        World: NewSpawn
        Name: '&5This is a crate'
        x: -88.5
        y: 72.0
        z: 267.5
    EDIT: Thanks to @Jeyge i got it to work, the problem was i was loading the world before the multiverse plugin was enabled so the world didn't exist until that was loaded so what i ended up doing was making a command that reloads the holograms instead
     
    Last edited: Sep 3, 2017
Thread Status:
Not open for further replies.

Share This Page