nullpointerexception on getConfig

Discussion in 'Plugin Development' started by Meowlan, Feb 16, 2021.

  1. Offline

    Meowlan

    Hello! I am trying to make an plugin that "crafts" a new item when i shift rightclick on a cauldron with 2 items that are specified in the config inside it. My code in the craft class is:
    Code:
    package de.meowlan.methblock.craft;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import de.meowlan.methblock.main.*;
    
    public class Cauldron implements Listener {
    
        private static Main plugin;
    
        public Cauldron(Main plugin) {
        }
    
        @EventHandler
        public void onEntityInteract(PlayerInteractEvent out) {
    
            final Player e = out.getPlayer();
            final Block b = e.getTargetBlock(null, 5);
            final Location loc = b.getLocation();
            if (out.getAction() == Action.RIGHT_CLICK_BLOCK && e.isSneaking() && b.getType().equals(Material.CAULDRON)) {
                for (Entity selE : Bukkit.getWorld("Hub").getEntities()) {
                    if (selE instanceof Item && selE.getLocation().getBlock().getLocation().equals(loc)) {
                        for (int x = 1; x <= plugin.getConfig().getKeys(false).size(); x++) {
                            if (((Item) selE).getItemStack().getType().equals(Material.getMaterial(plugin.getConfig().getStringList("recipe" + x).get(0).toString()))) {
                                e.sendMessage(
                                        "&7Debug1: " + plugin.getConfig().getStringList("recipe" + x).get(0).toString());
                            }
                            if (((Item) selE).getItemStack().getType().equals(Material.getMaterial(plugin.getConfig().getStringList("recipe" + x).get(1).toString()))) {
                                e.sendMessage(
                                        "&7Debug2: " + plugin.getConfig().getStringList("recipe" + x).get(1).toString());
                            }
    
                        }
                    }
                }
            }
        }
    }
    
    and in the main class:
    Code:
    package de.meowlan.methblock.main;
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import de.meowlan.methblock.craft.Cauldron;
    
    public class Main extends JavaPlugin {
    
        private static Main plugin;
    
        @Override
        public void onEnable() {
    
            plugin = this;
    
            getServer().broadcastMessage("§3§lMethBlock §7started and loaded §8"
                    + String.valueOf(plugin.getConfig().getKeys(false).size()) + " §7recipes!");
    
            FileConfiguration config = this.getConfig();
    
            List<String> recipe1 = Arrays.asList("Input1", "Input2", "Output");
    
            config.addDefault("cauldron_recipe1", recipe1);
            config.options().copyDefaults(true);
            saveConfig();
    
            PluginManager pluginManager = Bukkit.getPluginManager();
            pluginManager.registerEvents(new Cauldron(null), this);
        }
    
        public static Main getPlugin() {
            return plugin;
        }
    
    }
    
    But when i shift right click on the cauldron with items inside, it gives me this:
    Code:
    Caused by: java.lang.NullPointerException
    at de.meowlan.methblock.craft.Cauldron.onEntityInteract(Cauldron.java:32) ~[?:?]
    Please help!
    Sorry for my bad english and coding skills. Im german and just started with java like 2 days ago.

    Thanks in advance!




     
  2. Offline

    timtower Administrator Administrator Moderator

    @Meowlan
    private static Main plugin;
    Remove the static, give it a value
     
  3. Offline

    Meowlan

    What value should i give it? Something like private Main plugin = null;?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Meowlan Probably the value in the constructor?
     
  5. Offline

    Meowlan

    Im very confused wich constructor and wich value of that? im sorry im stupid
     
  6. Offline

    timtower Administrator Administrator Moderator

    Code:
    public Cauldron(Main plugin) {
    }
    You have a plugin, you just don't use it.
     
  7. Offline

    Meowlan

    Could you please just send me the fixed code? Im really stupid right now and cant figure it out
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    Meowlan

    Do you mean something like this?
    Code:
        private Main plugin;
       
        public Cauldron(Main plugin) {
            this.plugin = plugin;
        }
    
    that still gives me an nullpointerexception
     
  10. Offline

    timtower Administrator Administrator Moderator

    @Meowlan Then find out what is null
     
  11. Offline

    Meowlan

    Code:
                        for (int x = 1; x <= plugin.getConfig().getKeys(false).size(); x++) {
    this line is null so it probally cant get the config

    But how do i fix it?
     
    Last edited: Feb 16, 2021
  12. Offline

    Strahan

    You are passing null to the Cauldron constructor; of course it's going to puke.

    Also you really need to stop hanging code off of methods that can be null. You should also not embed the color character; use ChatColor.

    I'd also strongly recommend you learn Java before making plugins. It will save you from a lot of headaches. The problems you are struggling with are like the basics of basics.
     

Share This Page