NullPointerException

Discussion in 'Plugin Development' started by Jncwhite01, Jan 10, 2017 at 3:39 PM.

  1. Offline

    Jncwhite01

    Ive been trying to make a plugin which basically checks on block break if the block is in the config and if it is then has a random chance to give the player an item from config and have a null pointer exception on line 21 and i am not the most experienced with Java still learning from thenewbostons videos and from my computer science GCSE so any help and an explanation would be appreciated!

    The line the nullpointerexception given on is this:
    if(plugin.getConfig().getConfigurationSection("Items").contains(block.toString()))

    Code:
    package me.jncwhite.luckydrops;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class Events implements Listener
    {
        LuckyDrops plugin;
     
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event)
        {
            Player player = event.getPlayer();
            Material block = event.getBlock().getType();
            player.sendMessage("1");
         
            if(plugin.getConfig().getConfigurationSection("Items").contains(block.toString()))
            {
                player.sendMessage("2");
             
                int chance = plugin.getConfig().getConfigurationSection("Items." + block).getInt("Chance");
             
                ItemStack item = new ItemStack(Material.getMaterial(plugin.getConfig().getString("Items." + block + ".Item")),
                        plugin.getConfig().getInt("Items." + block + ".Amount"));
             
             
                double d = Math.random();
             
                if(d > chance/100)
                {
                    player.sendMessage("3");
                    player.getInventory().addItem(item);
                    return;
                }else{
                    return;
                }
            }
        }
    }
    
     
  2. @Jncwhite01
    plugin is null. Create a constructor for the class and set plugin equal to the main class's instance.
     
  3. Offline

    Jncwhite01

    Thank you:D I acctually know how to do that aha, hopefully will work after doing that.

    I fixed the plugin and had it working fine and randomly without changing any code it started throwing an error saying can not pass block break event, even though the same code worked fine before

    Code:
    package me.jncwhite.luckydrops;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class Events implements Listener
    {
        LuckyDrops plugin;
       
        public Events(LuckyDrops instance)
        {
            plugin = instance;
        }
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event)
        {
            Player player = event.getPlayer();
            Material block = event.getBlock().getType();
            player.sendMessage("1");
           
            if(plugin.getConfig().getConfigurationSection("Items").contains(block.toString()))
            {
                player.sendMessage("2");
               
                int chance = plugin.getConfig().getConfigurationSection("Items." + block).getInt("Chance");
               
                ItemStack item = new ItemStack(Material.getMaterial(plugin.getConfig().getString("Items." + block + ".Item")),
                        plugin.getConfig().getInt("Items." + block + ".Amount"));
               
                player.sendMessage("2");
                double d = Math.random();
                player.sendMessage(String.valueOf(d));
               
                if(chance/100 >= d)
                {
                    player.sendMessage("3");
                    player.getInventory().addItem(item);
                }
            }
        }
    }
    
    Thats the code now which randomly started giving this error

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 11, 2017 at 11:54 AM
  4. Offline

    kameronn

    @Jncwhite01
    Please post error message

    Also you can change this
    Code:
         if(plugin.getConfig().getConfigurationSection("Items").contains(block.toString()))
            {
    to this
    Code:
    if(plugin.getConfig().isSet("Items." + block.toString()) {
    // stuff here
    }
     
  5. Offline

    Jncwhite01

    Oh yehh sorry thought I had posted the stack trace and, I knew about the isSet way but wernt sure exactly how to do it aha

    StackTrace:

    Code:
    [20:00:11] [Server thread/INFO]: Starting minecraft server version 1.11.2
    [20:00:11] [Server thread/INFO]: Loading properties
    [20:00:11] [Server thread/INFO]: Default game type: SURVIVAL
    [20:00:11] [Server thread/INFO]: This server is running CraftBukkit version git-Spigot-7d78b81-f9e0440 (MC: 1.11.2) (Implementing API version 1.11.2-R0.1-SNAPSHOT)
    [20:00:11] [Server thread/INFO]: Debug logging is disabled
    [20:00:11] [Server thread/INFO]: Server Ping Player Sample Count: 12
    [20:00:11] [Server thread/INFO]: Using 4 threads for Netty based IO
    [20:00:11] [Server thread/INFO]: Generating keypair
    [20:00:12] [Server thread/INFO]: Starting Minecraft server on *:25565
    [20:00:12] [Server thread/INFO]: Using default channel type
    [20:00:12] [Server thread/INFO]: Set PluginClassLoader as parallel capable
    [20:00:12] [Server thread/INFO]: [LuckyDrops] Loading LuckyDrops v1.0
    [20:00:12] [Server thread/INFO]: **** Beginning UUID conversion, this may take A LONG time ****
    [20:00:12] [Server thread/INFO]: Preparing level "world"
    [20:00:12] [Server thread/INFO]: -------- World Settings For [world] --------
    [20:00:12] [Server thread/INFO]: Item Merge Radius: 2.5
    [20:00:12] [Server thread/INFO]: Item Despawn Rate: 6000
    [20:00:12] [Server thread/INFO]: Arrow Despawn Rate: 1200
    [20:00:12] [Server thread/INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [20:00:12] [Server thread/INFO]: View Distance: 10
    [20:00:12] [Server thread/INFO]: Custom Map Seeds:  Village: 10387312 Feature: 14357617
    [20:00:12] [Server thread/INFO]: Mob Spawn Range: 4
    [20:00:12] [Server thread/INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [20:00:12] [Server thread/INFO]: Random Lighting Updates: false
    [20:00:12] [Server thread/INFO]: Structure Info Saving: true
    [20:00:12] [Server thread/INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
    [20:00:12] [Server thread/INFO]: Cactus Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: Cane Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: Melon Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: Mushroom Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: Pumpkin Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: Sapling Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: Wheat Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: NetherWart Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: Vine Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: Cocoa Growth Modifier: 100%
    [20:00:12] [Server thread/INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [20:00:12] [Server thread/INFO]: Max TNT Explosions: 100
    [20:00:12] [Server thread/INFO]: Tile Max Tick Time: 50ms Entity max Tick Time: 50ms
    [20:00:12] [Server thread/INFO]: Experience Merge Radius: 3.0
    [20:00:12] [Server thread/INFO]: Nerfing mobs spawned from spawners: false
    [20:00:12] [Server thread/INFO]: Zombie Aggressive Towards Villager: true
    [20:00:13] [Server thread/INFO]: -------- World Settings For [world_nether] --------
    [20:00:13] [Server thread/INFO]: Item Merge Radius: 2.5
    [20:00:13] [Server thread/INFO]: Item Despawn Rate: 6000
    [20:00:13] [Server thread/INFO]: Arrow Despawn Rate: 1200
    [20:00:13] [Server thread/INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [20:00:13] [Server thread/INFO]: View Distance: 10
    [20:00:13] [Server thread/INFO]: Custom Map Seeds:  Village: 10387312 Feature: 14357617
    [20:00:13] [Server thread/INFO]: Mob Spawn Range: 4
    [20:00:13] [Server thread/INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [20:00:13] [Server thread/INFO]: Random Lighting Updates: false
    [20:00:13] [Server thread/INFO]: Structure Info Saving: true
    [20:00:13] [Server thread/INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
    [20:00:13] [Server thread/INFO]: Cactus Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Cane Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Melon Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Mushroom Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Pumpkin Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Sapling Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Wheat Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: NetherWart Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Vine Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Cocoa Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [20:00:13] [Server thread/INFO]: Max TNT Explosions: 100
    [20:00:13] [Server thread/INFO]: Tile Max Tick Time: 50ms Entity max Tick Time: 50ms
    [20:00:13] [Server thread/INFO]: Experience Merge Radius: 3.0
    [20:00:13] [Server thread/INFO]: Nerfing mobs spawned from spawners: false
    [20:00:13] [Server thread/INFO]: Zombie Aggressive Towards Villager: true
    [20:00:13] [Server thread/INFO]: -------- World Settings For [world_the_end] --------
    [20:00:13] [Server thread/INFO]: Item Merge Radius: 2.5
    [20:00:13] [Server thread/INFO]: Item Despawn Rate: 6000
    [20:00:13] [Server thread/INFO]: Arrow Despawn Rate: 1200
    [20:00:13] [Server thread/INFO]: Allow Zombie Pigmen to spawn from portal blocks: true
    [20:00:13] [Server thread/INFO]: View Distance: 10
    [20:00:13] [Server thread/INFO]: Custom Map Seeds:  Village: 10387312 Feature: 14357617
    [20:00:13] [Server thread/INFO]: Mob Spawn Range: 4
    [20:00:13] [Server thread/INFO]: Entity Activation Range: An 32 / Mo 32 / Mi 16
    [20:00:13] [Server thread/INFO]: Random Lighting Updates: false
    [20:00:13] [Server thread/INFO]: Structure Info Saving: true
    [20:00:13] [Server thread/INFO]: Entity Tracking Range: Pl 48 / An 48 / Mo 48 / Mi 32 / Other 64
    [20:00:13] [Server thread/INFO]: Cactus Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Cane Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Melon Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Mushroom Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Pumpkin Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Sapling Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Wheat Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: NetherWart Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Vine Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Cocoa Growth Modifier: 100%
    [20:00:13] [Server thread/INFO]: Hopper Transfer: 8 Hopper Check: 8 Hopper Amount: 1
    [20:00:13] [Server thread/INFO]: Max TNT Explosions: 100
    [20:00:13] [Server thread/INFO]: Tile Max Tick Time: 50ms Entity max Tick Time: 50ms
    [20:00:13] [Server thread/INFO]: Experience Merge Radius: 3.0
    [20:00:13] [Server thread/INFO]: Nerfing mobs spawned from spawners: false
    [20:00:13] [Server thread/INFO]: Zombie Aggressive Towards Villager: true
    [20:00:13] [Server thread/INFO]: Preparing start region for level 0 (Seed: -7580095699295984733)
    [20:00:14] [Server thread/INFO]: Preparing spawn area: 4%
    [20:00:15] [Server thread/INFO]: Preparing spawn area: 90%
    [20:00:15] [Server thread/INFO]: Preparing start region for level 1 (Seed: -7580095699295984733)
    [20:00:16] [Server thread/INFO]: Preparing spawn area: 66%
    [20:00:16] [Server thread/INFO]: Preparing start region for level 2 (Seed: -7580095699295984733)
    [20:00:17] [Server thread/INFO]: [LuckyDrops] Enabling LuckyDrops v1.0
    [20:00:17] [Server thread/INFO]: [LuckyDrops] Plugin Loaded
    [20:00:17] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [20:00:17] [Server thread/INFO]: Done (4.811s)! For help, type "help" or "?"
    [20:00:39] [User Authenticator #1/INFO]: UUID of player XvXRainbowzZ3D is bb67ce79-0933-40d1-93cc-b1098456005f
    [20:00:40] [Server thread/INFO]: XvXRainbowzZ3D[/127.0.0.1:58917] logged in with entity id 255 at ([world]91.30000001192093, 62.0, 252.69999998807907)
    [20:00:52] [Server thread/ERROR]: Could not pass event BlockBreakEvent to LuckyDrops v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-7d78b81-f9e0440]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-7d78b81-f9e0440]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.PlayerInteractManager.breakBlock(PlayerInteractManager.java:291) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.PlayerInteractManager.a(PlayerInteractManager.java:218) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:842) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.PacketPlayInBlockDig.a(SourceFile:40) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.PacketPlayInBlockDig.a(SourceFile:10) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_112]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_112]
        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574) [spigot.jar:git-Spigot-7d78b81-f9e0440]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_112]
    Caused by: java.lang.NullPointerException
        at org.bukkit.inventory.ItemStack.<init>(ItemStack.java:68) ~[spigot.jar:git-Spigot-7d78b81-f9e0440]
        at me.jncwhite.luckydrops.Events.onBlockBreak(Events.java:32) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_112]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_112]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_112]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-7d78b81-f9e0440]
        ... 17 more
     
    Last edited by a moderator: Jan 11, 2017 at 1:16 PM
  6. @Jncwhite01

    Something on that line is null, figure out what it is.
     
  7. Offline

    kameronn

    @Jncwhite01
    I'm pretty sure
    Code:
    Material block = event.getBlock().getType();
    Isn't a string and pretty sure from previous experience it wouldnt print something as clean as you would think so try this
    Code:
    Material blocktype = event.getBlock().getType();
    String block = blocktype.tostring
    block = block .replace("!!org.bukkit.Material", "");
    block = block .replaceAll("'", "");
    Lastly use .equalsIgnoreCase("");
    instead of .contains because then materials with similar names would have problems.
     
  8. Offline

    timtower Ninja on the waves Moderator

  9. Offline

    Jncwhite01

    Thanks for the replies, will edit the plugin when I get back on laptop and try get it working
     

Share This Page