构造函数参数的NullPointerException

时间:2014-09-28 18:28:47

标签: java bukkit

我在类方法上遇到NullPointerException时遇到了很多困难。问题是,当我调用在构造函数上初始化的变量时,它们返回null。请记住,KitGUI扩展了GUI。

package src.foundation.core.game.kit;

import org.bukkit.entity.Player;

import src.foundation.core.GUI;
import src.foundation.core.game.Game;
import src.foundation.core.game.Kit;
import src.foundation.core.util.ItemStackFactory;

public class KitGUI extends GUI
{
    private Game game;
    private Player player;

    public KitGUI(Game game, Player player)
    {
        super(player, "Select Kit", 27);
        this.game = game;
        this.player = player;
    }

    public void addItems()
    {
        for (Kit kit : this.game.getKits())
        {
            // NullPointerException...
            addMenuItem(ItemStackFactory.createItem(kit.getDisplay()));
        }
    }

    public void checkItems()
    {
        for (Kit kit : game.getKits())
        {
            if (getClick().getItemMeta().getDisplayName().contains(kit.getName()))
            {
                game.selectKit(getPlayer(), kit);
                break;
            }
        }
    }
}

GUI类:

package src.foundation.core;

import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public abstract class GUI extends Module
{
    private String name;
    private int slots;

    private Inventory inventory;

    private Player player;
    private ItemStack choice;

    public GUI(Player player, String name, int slots)
    {
        super("GUI");

        this.name = name;
        this.slots = slots;
        this.player = player;

        this.inventory = Bukkit.createInventory(null, this.slots, this.name);

        addItems();

        this.player.openInventory(inventory);
    }

    public abstract void addItems();

    public abstract void checkItems();

    public void addMenuItem(ItemStack item)
    {
        this.inventory.addItem(item);
    }

    public Inventory getInventory()
    {
        return this.inventory;
    }

    public ItemStack getClick()
    {
        return this.choice;
    }

    public Player getPlayer()
    {
        return this.player;
    }

    @EventHandler
    public void selectChoice(InventoryClickEvent e)
    {
        this.player = (Player) e.getWhoClicked();
        this.choice = e.getCursor();

        e.setCancelled(true);

        if (!(e.getInventory().getName().equals(this.name)))
            return;

        if (e.getCurrentItem() == null || !e.getCurrentItem().hasItemMeta() || !e.getCurrentItem().getItemMeta().hasDisplayName())
            return;

        if (e.getCursor().getItemMeta() == null)
            return;

        if (e.isCancelled() == true)
        {
            checkItems();

            this.player.playSound(this.player.getLocation(), Sound.CHICKEN_EGG_POP, 1.0F, 1.0F);

            this.player.closeInventory();
        }
    }
}

堆栈追踪:

Caused by: java.lang.NullPointerException
        at src.foundation.core.game.kit.KitGUI.addItems(KitGUI.java:25) ~[?:?]
        at src.foundation.core.GUI.<init>(GUI.java:32) ~[?:?]
        at src.foundation.core.game.kit.KitGUI.<init>(KitGUI.java:18) ~[?:?]
        at src.foundation.core.game.GameManager.lobbyItemsUse(GameManager.java:2
68) ~[?:?]

关于问题出在何处的任何想法?问候。

0 个答案:

没有答案