简体   繁体   中英

Get players inventory and then broadcast the item displayname

Ok, Im trying to check the player's inventory when they login and it it say that displayname

public void onPlayerJoin(PlayerJoinEvent event) {
    Player p = event.getPlayer();

    for (int i = 0; i < p.getInventory().getSize(); i++) {
        if (p.getInventory().getItem(i).getType() != null) {
            if (!p.getInventory().getItem(i).getItemMeta().getDisplayName().equals("KITS")){
                Bukkit.broadcastMessage(i.getItemMeta().getDisplayName());
            }
        }
    }
}

It's just causing error.

Error log:

Caused by: java.lang.NullPointerException
    at me.BlueJelly.MPSKitPvP.Listeners.PlayerListener.onPlayerJoin(PlayerListener.jav
a:31) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_09]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_09]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_09]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_09]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[
   craftbukkit.jar:git-Bukkit-1.6.4-R2.0-9-g68d0e57-b2930jnks]

I know it's a NullPointerException but i don't know how it can say that. I hope you understands what i wrote...

根据文档 ,我相信应该使用p.getInventory().getItem(i)!= null代替p.getInventory().getItem(i).getType() != null p.getInventory().getItem(i)!= null ,好像其中没有项目堆栈试图获取其类型的插槽将抛出一个npe

There are many potential places in your code for a NullPointerException.

For example:

p.getInventory().getItem(i).getType() 

Use the reported line number (31) to find the exact line. If you can't debug the code, use logs (or simple souts) to find the null reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM