Solved forEach error

Discussion in 'Spigot Plugin Development' started by ytTobyo, Jan 18, 2020.

  1. Hello Communtity,
    i make a plugin but i get this error, can somebody tell me whats wrong
    Code (Text):
    [17:28:05 WARN]: [derpcraftlegendary] Task #8 for derpcraftlegendary v1.0 generated an exception
    java.lang.NullPointerException: null
            at Intensity.Tobyo.listener.INV$1.lambda$0(INV.java:33) ~[?:?]
            at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?]
            at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1083) ~[?:?]
            at Intensity.Tobyo.listener.INV$1.run(INV.java:20) ~[?:?]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:76) ~[spigot.jar:git-Spigot-2cf50f0-8166d17]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:361) [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at java.lang.Thread.run(Thread.java:834) [?:?]

    Code (Text):
    package Intensity.Tobyo.listener;

    import java.util.ArrayList;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import Intensity.Tobyo.Intensity;

    public class INV {

      public static ArrayList<Player> speed = new ArrayList<>(), Invisibility = new ArrayList<>(),
          Strength = new ArrayList<>(), NV = new ArrayList<>();

      public static void onCheck() {
        Bukkit.getScheduler().scheduleSyncRepeatingTask(Intensity.getInstance(), new Runnable() {

          @Override
          public void run() {
            Bukkit.getOnlinePlayers().forEach(p -> {
             
              if (speed.contains(p)) {
                if (p.getInventory().getHelmet() == null) {
                  speed.remove(p);
                  p.removePotionEffect(PotionEffectType.SPEED);

                } else {
                  if (p.getInventory().getHelmet().getItemMeta().hasLore()) {

                  }
                }
              } else {
                if (p.getInventory().getHelmet().getItemMeta().hasLore()) {
                  speed.add(p);
                  p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000000, 2));
                }
              }

            });
          }

        }, 20, 20);

      }
    }
     

    The Error comes from lines 75 it is
    so whats wrong there?
    The error comes only when the player has not the helmet on
    And how i can better check if a player has the armor on?
     
  2. DavidDevelops

    Supporter

    If you understand that code you should be able to read an error...
    Code (Text):
    java.lang.NullPointerException: null
    Its saying its null?
     
  3. I know but for that i made this :

     
  4. Please learn to read stacktraces, these are the lines of the NPE, post these lines
     
  5. It's most likely just this line:

    if (p.getInventory().getHelmet().getItemMeta().hasLore()) {

    I'm not sure of the nullity of Inventory#getHelmet but perhaps that could be null. If it's not null then the item might not have an item meta so you have to determine whether it does or not prior to calling getItemMeta, try ItemStack#hasItemMeta()

    ie:
    Code (Java):
    if the item has an item meta {
        var meta = get item meta
        if meta.hasLore() {
            var lore = meta.getLore
        }
    }
    Always check for nullable method returns before using them :p
     
  6. I know the rror now i forgott to ckeck after the else is the helemt null or not.
    I forgott this than the error come