Solved I need help... NPE

Discussion in 'Spigot Plugin Development' started by GB_6, May 10, 2017.

  1. Hello everyone, I need some help solving a Null Pointer Exception.

    Code (Text):
    10.05 13:22:54 [Server] ERROR Could not pass event BlockPlaceEvent to CustomTNT v1.0 10.05 13:22:54 [Server] INFO org.bukkit.event.EventException 10.05 13:22:54 [Server] INFO at$1.execute( ~[1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at co.aikar.timings.TimedEventExecutor.execute( ~[1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent( ~[1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callBlockPlaceEvent( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.ItemStack.placeItem( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at java.util.concurrent.Executors$ [?:1.8.0_121] 10.05 13:22:54 [Server] INFO at [?:1.8.0_121] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A( [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at [1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"] 10.05 13:22:54 [Server] INFO at [?:1.8.0_121] 10.05 13:22:54 [Server] INFO Caused by: java.lang.NullPointerException 10.05 13:22:54 [Server] INFO at com.gb6.customtnt.Utils.addCustom( ~[?:?] 10.05 13:22:54 [Server] INFO at ~[?:?] 10.05 13:22:54 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121] 10.05 13:22:54 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke( ~[?:1.8.0_121] 10.05 13:22:54 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_121] 10.05 13:22:54 [Server] INFO at java.lang.reflect.Method.invoke( ~[?:1.8.0_121] 10.05 13:22:54 [Server] INFO at$1.execute( ~[1-8-8-paperspigot.jar:git-PaperSpigot-"8b18730"
    BlockPlaceEvent Code:
    Code (Text):
    public class BlockPlacedEvent implements Listener {

        public void onBlockPlace(BlockPlaceEvent e) {
            if (e.isCancelled()) return;
            Utils.addCustom(e.getBlock(), e.getItemInHand(), e.getPlayer());

    Utils.addCustom Code:

    Code (Text):
    public static void addCustom(Block bl, ItemStack b, Player player) {
            if (b.hasItemMeta() && b.getItemMeta().hasLore()) {
                List<String> lore = b.getItemMeta().getLore();
                String Flore = lore.get(0);
                if (Flore.equals(TNTInfo.getDenseL())) TNTManager.addTNT(player, "DENSE", bl.getLocation());
                if (Flore.equals(TNTInfo.getNuclearL())) TNTManager.addTNT(player, "NUCLEAR", bl.getLocation());
                if (Flore.equals(TNTInfo.getSilencedL())) TNTManager.addTNT(player, "SILENCED", bl.getLocation());
                if (Flore.equals(TNTInfo.getBiohazardL())) TNTManager.addTNT(player, "BIOHAZARD", bl.getLocation());
                if (Flore.equals(TNTInfo.getLuckyL())) TNTManager.addTNT(player, "LUCKY", bl.getLocation());

    TNTManager.addTNT Code:
    Code (Text):
    public void addTNT(Player player, String type, Location location) {
            if (placedTNT.containsKey(player.getUniqueId())) {
                Map<Location, String> tnt = placedTNT.get(player.getUniqueId());
                tnt.put(location, type);
                placedTNT.put(player.getUniqueId(), tnt);
            } else {
                placedTNT.put(player.getUniqueId(), new HashMap<Location, String>() {

                    private static final long serialVersionUID = 1L;
                        put(location, type);
    I know I still need to clean up code...
    Thanks in advance!
    #1 GB_6, May 10, 2017
    Last edited: May 10, 2017
  2. You never initialized your "util" variable.
  3. Mas


    ^ Also, utility classes can just contain static methods to easily access whatever functions you need, no need to create instances in this case.
    • Optimistic Optimistic x 1
  4. Methods*
  5. Mas


    They're synonymous.
  6. That didnt fix it, I changed the code to what i have now, now the NPE is pointing to one of the if lines in addCustom
    #6 GB_6, May 10, 2017
    Last edited: May 10, 2017
  7. Mas


    Show us the error and your updated code.
  8. I just said I changed the code, NPE is in the post aswell as the code that I changed @SpiroMarshes
    #8 GB_6, May 10, 2017
    Last edited: May 10, 2017
  9. I don't like doing bumps but I'm kind off in a hurry. Hope you understand it.
  10. Give us the full stacktrace

    Utility class patterns break object oriented design and should be avoided as much as possible.
  11. Im sorry but I don't understand that ^ I will provide the full stacktrace asap
  12. Mas


    Go say that to Apache Commons, Google, and even Java itself, who, guess what? All use utility classes. Could you. Find me one Java project medium-large in size with no utility classes?
    Not sure where you learnt that. Sounds like you're just spouting out worrds to sound smart.
  13. What is line 63 in

    That's where the NPE is.
  14. Serializator


    Can you verify that BlockPlaceEvent#getItemInHand() and BlockPlaceEvent#getBlock() both don't return null?
  15. Line 63 is one if the if statements. In addCustom
    One of these :
    Code (Text):
    if (Flore.equals(TNTInfo.getDenseL())) TNTManager.addTNT(player, "DENSE", bl.getLocation());
  16. They can't return null, I already tried adding a null check but that didn't help.
  17. Ok, something in there is null. To narrow down your search use the trick of spreading that line out across multiple lines.

    Code (Text):
    if (Flore
    , bl
    It will still throw an NPE, but it will be a new line number that is more specific because everything has its own line.

    Side note: this is why it's good practice to never same-line your if statements and your one-liner conditions. Always use brackets, and go to the next line as much as possible. It makes things much easier to debug when things go wrong. Clean code is happy code. ;)

    And remember that NPE means you called a method/field on a null object. So if getLocation() throws the NPE then that means bl is null.

    Point of advice, null check everything before you use/access it. It doesn't add too much overhead and makes for cleaner code with less errors.
    #17 BillyGalbreath, May 11, 2017
    Last edited: May 11, 2017
    • Useful Useful x 1
  18. Wow I never thought of splitting the lines, thank you for that tip. I will do this when I get home from school. Btw awesome beard.
    • Friendly Friendly x 1
  19. Well to more simply break it down, if you're getting an NPE from dereferencing, then it would only happen when you use the dereference operator (the '.').

    So look for what you use that on:

    Code (Java):
    if (Flore.equals(TNTInfo.getDenseL())) TNTManager.addTNT(player, "DENSE", bl.getLocation());
    • Flore
    • TNTInfo
    • TNTManager
    • bl

    Since you name variables uppercase sometimes (e.g. Flore), I can't be certain that the TNTInfo and TNTManager are direct class reference (vs say, field names). I'm actually going to assume they're fields, because #addTNT is not static.

    So look for any of those to be null via debug.