Błąd NullPointerException w moim własnym pluginie

Zombie
minecraft, pluginy
PszemoPL
PszemoPL

Mój plugin na efekty powoduje że jak wyrzucę itemki z eq to wyskakuje taki error. Pomoże ktoś?

        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[mc.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[mc.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [mc.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1776) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInSetCreativeSlot.a(SourceFile:23) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInSetCreativeSlot.a(SourceFile:9) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [mc.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_191]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_191]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [mc.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
Caused by: java.lang.NullPointerException
        at pl.pszemopl.efekty.events.InventoryClickListener.onClickMain(InventoryClickListener.java:20) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_191]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_191]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[mc.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
[22:01:14 ERROR]: Could not pass event InventoryCreativeEvent to pPLextraefekty v1.0
org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[mc.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[mc.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [mc.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1776) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInSetCreativeSlot.a(SourceFile:23) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInSetCreativeSlot.a(SourceFile:9) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [mc.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_191]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_191]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [mc.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [mc.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
Caused by: java.lang.NullPointerException
        at pl.pszemopl.pplextraefekty.events.InventoryClickListener.onClickMain(InventoryClickListener.java:20) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_191]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_191]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[mc.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
Nieznajomy11
Nieznajomy11 Moderator forum.lvlup.pro

Potrzebne jest źródło, żeby powiedzieć coś więcej odnośnie do błędu. Gdzieś w onClickMain jakiś getter (strzelam, że pewnie getInventory lub podobny) zwraca nulla.

Kamilkime
Kamilkime Regularny

https://www.spigotmc.org/threads/tutorial-how-to-read-stacktraces-solve-most-of-your-plugin-problems-by-yourself.48121/

Polecam przeczytanie sobie poradnika o odczytywaniu błędu, dobrze jest to umieć =)

PszemoPL
PszemoPL

Tak wyglądaa kod w funkcji "onClickMain"

public class InventoryClickListener implements Listener {
    @SuppressWarnings("deprecation")
	@EventHandler
    public void onClickMain(InventoryClickEvent event) {
        Player p = (Player) event.getWhoClicked();
        if (ChatUtil.fixColor(Config.NAME).equalsIgnoreCase(event.getClickedInventory().getName())) {
            event.setCancelled(true);
            if (event.getSlot() == Config.SPEED_SLOT) {
                if (p.getInventory().containsAtLeast(new ItemStack(Material.getMaterial(Config.SPEED_CURRENCY)), Config.SPEED_AMOUNT)) {
                    p.getInventory().removeItem(new ItemStack(Material.getMaterial(Config.SPEED_CURRENCY), Config.SPEED_AMOUNT));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Config.SPEED_DURATION, 1, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, Config.SPEED_DURATION, 1, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Config.SPEED_DURATION, 0, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Config.SPEED_DURATION1, 0, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, Config.SPEED_DURATION1, 0, true));
                    ChatUtil.sendMessage(p, Config.SPEED_BUYMESS);
                    Bukkit.getOnlinePlayers().forEach(all -> ChatUtil.sendMessage(all, Config.SPEED_BUYBC.replace("{PLAYER}", p.getName())));
                    p.sendTitle(ChatUtil.fixColor(Config.SPEED_BUYTITLE), ChatUtil.fixColor(Config.SPEED_BUYSUBTITLE));
                } else {
                    ChatUtil.sendMessage(p, Config.SPEED_LACK);
                }
            }
            if (event.getSlot() == Config.STRENGTH_SLOT) {
                if (p.getInventory().containsAtLeast(new ItemStack(Material.getMaterial(Config.STRENGTH_CURRENCY)), Config.STRENGTH_AMOUNT)) {
                    p.getInventory().removeItem(new ItemStack(Material.getMaterial(Config.STRENGTH_CURRENCY), Config.STRENGTH_AMOUNT));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Config.STRENGTH_DURATION, 0, true));
                    ChatUtil.sendMessage(p, Config.STRENGTH_BUYMESS);
                    Bukkit.getOnlinePlayers().forEach(all -> ChatUtil.sendMessage(all, Config.STRENGTH_BUYBC.replace("{PLAYER}", p.getName())));
                    p.sendTitle(ChatUtil.fixColor(Config.STRENGTH_BUYTITLE), ChatUtil.fixColor(Config.STRENGTH_BUYSUBTITLE));
                } else {
                    ChatUtil.sendMessage(p, Config.STRENGTH_LACK);
                }
            }
            if (event.getSlot() == Config.HASTE_SLOT) {
                if (p.getInventory().containsAtLeast(new ItemStack(Material.getMaterial(Config.HASTE_CURRENCY)), Config.HASTE_AMOUNT)) {
                    p.getInventory().removeItem(new ItemStack(Material.getMaterial(Config.HASTE_CURRENCY), Config.HASTE_AMOUNT));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, Config.HASTE_DURATION, 0, true));
                    ChatUtil.sendMessage(p, Config.HASTE_BUYMESS);
                    Bukkit.getOnlinePlayers().forEach(all -> ChatUtil.sendMessage(all, Config.HASTE_BUYBC.replace("{PLAYER}", p.getName())));
                    p.sendTitle(ChatUtil.fixColor(Config.HASTE_BUYTITLE), ChatUtil.fixColor(Config.HASTE_BUYSUBTITLE));
                } else {
                    ChatUtil.sendMessage(p, Config.HASTE_LACK);
                }
            }
        }
    }
}
Nieznajomy11
Nieznajomy11 Moderator forum.lvlup.pro

Musisz podać całą klasę InventoryClickListener, bez żadnych zmian.

PszemoPL
PszemoPL

no to jest

Nieznajomy11
Nieznajomy11 Moderator forum.lvlup.pro

Zgodnie z kodem, który wkleiłeś 20 linia to:

ChatUtil.sendMessage(p, Config.SPEED_LACK);

Nie widzę, żeby coś tutaj mogło wyrzucić NPE.

Kamilkime
Kamilkime Regularny

To może policz linijki u siebie w kodzie?

PszemoPL
PszemoPL

20 linijka to if (ChatUtil.fixColor(Config.NAME).equalsIgnoreCase(event.getClickedInventory().getName())) {

PszemoPL
PszemoPL

Proszę InventoryClickListener razem z importami

package pl.pszemopl.pplextraefekty.events;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import pl.pszemopl.pplextraefekty.configurations.Config;
import pl.pszemopl.pplextraefekty.utils.ChatUtil;

public class InventoryClickListener implements Listener {
    @SuppressWarnings("deprecation")
	@EventHandler
    public void onClickMain(InventoryClickEvent event) {
        Player p = (Player) event.getWhoClicked();
        if (ChatUtil.fixColor(Config.NAME).equalsIgnoreCase(event.getClickedInventory().getName())) {
            event.setCancelled(true);
            if (event.getSlot() == Config.SPEED_SLOT) {
                if (p.getInventory().containsAtLeast(new ItemStack(Material.getMaterial(Config.SPEED_CURRENCY)), Config.SPEED_AMOUNT)) {
                    p.getInventory().removeItem(new ItemStack(Material.getMaterial(Config.SPEED_CURRENCY), Config.SPEED_AMOUNT));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Config.SPEED_DURATION, 1, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, Config.SPEED_DURATION, 1, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Config.SPEED_DURATION, 0, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Config.SPEED_DURATION1, 0, true));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, Config.SPEED_DURATION1, 0, true));
                    ChatUtil.sendMessage(p, Config.SPEED_BUYMESS);
                    Bukkit.getOnlinePlayers().forEach(all -> ChatUtil.sendMessage(all, Config.SPEED_BUYBC.replace("{PLAYER}", p.getName())));
                    p.sendTitle(ChatUtil.fixColor(Config.SPEED_BUYTITLE), ChatUtil.fixColor(Config.SPEED_BUYSUBTITLE));
                } else {
                    ChatUtil.sendMessage(p, Config.SPEED_LACK);
                }
            }
            if (event.getSlot() == Config.STRENGTH_SLOT) {
                if (p.getInventory().containsAtLeast(new ItemStack(Material.getMaterial(Config.STRENGTH_CURRENCY)), Config.STRENGTH_AMOUNT)) {
                    p.getInventory().removeItem(new ItemStack(Material.getMaterial(Config.STRENGTH_CURRENCY), Config.STRENGTH_AMOUNT));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, Config.STRENGTH_DURATION, 0, true));
                    ChatUtil.sendMessage(p, Config.STRENGTH_BUYMESS);
                    Bukkit.getOnlinePlayers().forEach(all -> ChatUtil.sendMessage(all, Config.STRENGTH_BUYBC.replace("{PLAYER}", p.getName())));
                    p.sendTitle(ChatUtil.fixColor(Config.STRENGTH_BUYTITLE), ChatUtil.fixColor(Config.STRENGTH_BUYSUBTITLE));
                } else {
                    ChatUtil.sendMessage(p, Config.STRENGTH_LACK);
                }
            }
            if (event.getSlot() == Config.HASTE_SLOT) {
                if (p.getInventory().containsAtLeast(new ItemStack(Material.getMaterial(Config.HASTE_CURRENCY)), Config.HASTE_AMOUNT)) {
                    p.getInventory().removeItem(new ItemStack(Material.getMaterial(Config.HASTE_CURRENCY), Config.HASTE_AMOUNT));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, Config.HASTE_DURATION, 0, true));
                    ChatUtil.sendMessage(p, Config.HASTE_BUYMESS);
                    Bukkit.getOnlinePlayers().forEach(all -> ChatUtil.sendMessage(all, Config.HASTE_BUYBC.replace("{PLAYER}", p.getName())));
                    p.sendTitle(ChatUtil.fixColor(Config.HASTE_BUYTITLE), ChatUtil.fixColor(Config.HASTE_BUYSUBTITLE));
                } else {
                    ChatUtil.sendMessage(p, Config.HASTE_LACK);
                }
            }
        }
    }
}
Kamilkime
Kamilkime Regularny

Jeśli gracz kliknie poza inventory - getClickedInventory() zwróci null I najlepiej wrzucaj kod np. na pastebin.com

PszemoPL
PszemoPL

okej

PszemoPL
PszemoPL

nie działa

Kamilkime
Kamilkime Regularny

No... Napisanie nie działa zupełnie nic mi nie mówi - jakieś błędy, kod, itd.

PszemoPL
PszemoPL
Kamilkime
Kamilkime Regularny

To teraz przeczytaj raz jeszcze co ci napisałem, co może być null'em :/

PszemoPL
PszemoPL

No getClickedInventory()

Kamilkime
Kamilkime Regularny

No, to teraz się zastanów co sprawdzasz tym warunkiem

system
system

Ten temat został automatycznie zamknięty 32 dni po ostatnim wpisie. Tworzenie nowych odpowiedzi nie jest już możliwe.