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( ~[?:?]
            at java.lang.Iterable.forEach( ~[?:?]
            at java.util.Collections$UnmodifiableCollection.forEach( ~[?:?]
            at Intensity.Tobyo.listener.INV$ ~[?:?]
            at ~[spigot.jar:git-Spigot-2cf50f0-8166d17]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat( [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D( [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D( [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C( [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at [spigot.jar:git-Spigot-2cf50f0-8166d17]
            at [?:?]

    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() {

          public void run() {
            Bukkit.getOnlinePlayers().forEach(p -> {
              if (speed.contains(p)) {
                if (p.getInventory().getHelmet() == null) {

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

              } else {
                if (p.getInventory().getHelmet().getItemMeta().hasLore()) {
                  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


    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()

    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