Nullpointer Exception on Move (again)

Discussion in 'Plugin Development' started by JoltTheBolt, Jan 6, 2022.

  1. Offline

    JoltTheBolt

    Code:
    [JoltItems] Task #19621 for JoltItems v1.0 generated an exception
    java.lang.NullPointerException
            at io.github.JoltMuz.joltitems.Jetpack$1.run(Jetpack.java:65) ~[?:?]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) ~[server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:723) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [server.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    My Jetpack Class:

    Code:
    public class Jetpack implements Listener
    {
        int fuel;
        HashMap<String, Integer> Fuels = new HashMap<>();
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e)
        {
            Fuels.put(e.getPlayer().getDisplayName(),0);
    
        }
        @EventHandler
        public void Move(PlayerMoveEvent e)
        {
    
            Player p = e.getPlayer();
    
            if (p.getGameMode() == GameMode.SURVIVAL){
                if (p.getInventory().getChestplate() != null)
                {
                    if (p.getInventory().getChestplate().getItemMeta().getDisplayName().equals(ChatColor.BLUE + ChatColor.BOLD.toString() + "Jetpack"))
                    {
                        new BukkitRunnable()
                        {
                            @Override
                            public void run()
                            {
                                {
                                    if(p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() != Material.AIR)
                                    {
                                        if (p.isSneaking())
                                        {
    
                                            if ((Integer)Fuels.get(e.getPlayer().getDisplayName()) < 100)
                                            {
                                                Fuels.put((e.getPlayer().getDisplayName()),Fuels.get(e.getPlayer().getDisplayName()) + 1);
                                                new ActionBar(ChatColor.YELLOW + ChatColor.BOLD.toString() + new String(new char[Fuels.get(e.getPlayer().getDisplayName()) / 5]).replace("\0", "▇")).sendToPlayer(p);
                                            }
                                            if ((Integer)Fuels.get(e.getPlayer().getDisplayName()) == 100)
                                            {
                                                new ActionBar(ChatColor.GOLD + ChatColor.BOLD.toString() + "Fuel Fully Recharged!").sendToPlayer(p);
                                                this.cancel();
                                            }
                                        }
                                        if ((Integer)Fuels.get(e.getPlayer().getDisplayName()) == 0)
                                        {
                                            if (p.isFlying())
                                                {p.setFlying(false);}
                                            p.setAllowFlight(false);
                                            new ActionBar(ChatColor.DARK_GRAY + ChatColor.BOLD.toString() + "Sneak to Recharge Fuel").sendToPlayer(p);
                                        }
                                    }
                                }
                            }
                        }.runTaskTimer(Main.plugin, 80L, 100L);
    
                        if (p.getVelocity().getY() > 0)
                        {
    
                            if ((Integer)Fuels.get(e.getPlayer().getDisplayName()) == 100)
                            {
                                p.setAllowFlight(true);
    
                            }
                        }
                    }
                }
            }
        }
        @EventHandler
        public void Fly(PlayerToggleFlightEvent e)
        {
            Player p = e.getPlayer();
            if (p.getGameMode() == GameMode.SURVIVAL){
                if ((Integer)Fuels.get(e.getPlayer().getDisplayName()) == 100)
                {
                    new BukkitRunnable()
                    {
                        @Override
                        public void run()
                        {
                            Fuels.put((e.getPlayer().getDisplayName()),Fuels.get(e.getPlayer().getDisplayName()) - 10);;
                            p.setVelocity(p.getVelocity().add(new Vector(0, 0.5, 0)));
                            p.setVelocity(p.getLocation().getDirection().multiply(1.5));
                            if (!p.isFlying()) {p.setAllowFlight(true);p.setFlying(true);}
                            new ActionBar(ChatColor.YELLOW + ChatColor.BOLD.toString() + new String(new char[(Integer)Fuels.get(e.getPlayer().getDisplayName()) / 5]).replace("\0", "▇")).sendToPlayer(p);
                            p.getWorld().playEffect(p.getLocation().add(new Vector(0,1,0)) ,Effect.LAVA_POP,  0);
                            p.getWorld().playEffect(p.getLocation().add(new Vector(0,1,0)) ,Effect.LAVADRIP,  0);
                            if ((Integer)Fuels.get(e.getPlayer().getDisplayName()) == 0)
                            {
                                p.setAllowFlight(false);
                                p.setFlying(false);
                                this.cancel();
                            }
                            if(p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() != Material.AIR)
                            {
                                p.setAllowFlight(false);
                                p.setFlying(false);
                                this.cancel();
                            }
                        }
                    }.runTaskTimer(Main.plugin, 0L, 2L);
                }
            }
        }
        @EventHandler
        public void FallDamageCancel(final EntityDamageEvent e)
        {
            if (!(e.getEntity() instanceof Player))
            {
                return;
            }
            Player p = (Player) e.getEntity();
            if (e.getCause() == EntityDamageEvent.DamageCause.FALL)
            {
                ItemStack chest =p.getInventory().getChestplate();
                if (chest.getType() != null && chest.getType() == Material.DIAMOND_CHESTPLATE)
                {
                    if (chest.hasItemMeta() &&
                            chest.getItemMeta().hasDisplayName() &&
                            chest.getItemMeta().getDisplayName().equals(ChatColor.BLUE + ChatColor.BOLD.toString() + "Jetpack")) {
    
                        e.setCancelled(true);
                    }
                }
            }
        }
    }
    
    Why!
    Line 65 seems to be

    Code:
     if ((Integer)Fuels.get(e.getPlayer().getDisplayName()) == 0)
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @JoltTheBolt
    getItemMeta().getDisplayName() that can return null I believe,
    Fuels does not have to contain the user.
    And why are you casting so often when it isn't needed?
     
  3. Offline

    JoltTheBolt

    Okay, will try. Thank you very much!

    But I want different fuel for every user, should I use UUID or are you suggesting something else?

    You mean in the runnable? I want it to show the player as it is increasing and the show it as it's decreasing too.
    Or do you mean that I should give value of player's fuel to another variable in event and use that variable?

    Edit: Orrr you might mean why I am writing (Integer) so many times, to which I didn't give much thought.

    Edit: Orr you can mean something else -> I am confused, sorry.
     
    Last edited: Jan 6, 2022
  4. Offline

    KarimAKL

    Correct. It is already an integer, so there's no need to cast it.

    Using UUIDs is always preferred over display names, as they are bound to the account and cannot be changed.
    However, I'm not sure what Tim is suggesting instead.
     
  5. Offline

    Strahan

    I assume Tim is referring to OP's habit of referencing the fuels map with the assumption that the user is already in it; not checking first.

    OP, you really should consider guard clauses. Methods should not look like arrowheads, heh.
     
    KarimAKL likes this.
  6. Offline

    JoltTheBolt

    Ah I see, I see. Thank you very much.

    Ah okay, thanks for the advice! Will try.
     

Share This Page