I'm trying to make a bukkit plugin, which implements backpacks in the game if the player has the correct item (Enchanted Normal book with Efficiency 10 and Unbreaking 10, to be uncraftable).

I want to save the inventory using the built-in YamlConfiguration feature in Bukkit.

I use a InventoryCloseEvent to see when the player closes the backpack, but I always get an error when I close the backpack.

    [12:28:27] [Server thread/ERROR]: Could not pass event InventoryCloseEvent to ServerAssets v1.0
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleInventoryCloseEvent(CraftEventFactory.java:838) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1384) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:18) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:1) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot.jar:git-Spigot-f928e7a-994b2aa]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
Caused by: java.lang.NullPointerException
    at com.isoStudios.main.BackPack.inventoryClose(BackPack.java:84) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-f928e7a-994b2aa]
    ... 16 more

The 84th row is:

File file = new File("plugins/ServerAssets/saves/" + player.getName() + ".yml");

Here is the source code for the BackPack class:

package com.isoStudios.main;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class BackPack implements CommandExecutor, Listener {

    YamlConfiguration config;

    public BackPack(YamlConfiguration config) {
        this.config = config;

    Inventory inventory = Bukkit.createInventory(null, 9, "BackPack");
    Player player;
    boolean opened = false;

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        boolean cancelled = true;

        if (sender instanceof Player) {
            player = (Player) sender;

            ItemStack[] inv = player.getInventory().getContents();
            for (ItemStack item : inv) {
                if (item != null) {
                    if (item.getEnchantmentLevel(Enchantment.DIG_SPEED) == 10 && item.getEnchantmentLevel(Enchantment.DURABILITY) == 10 && item.getType().equals(Material.BOOK)) {
                        File file = new File("plugins/ServerAssets/saves/" + player.getName() + ".yml");
                        inventory = Bukkit.createInventory(player, 9, "BackPack");
                        if (file.exists()) {
                            try {
                            } catch (Exception ex) {
                                Bukkit.getLogger().info("Failed to load config file, while trying to restore backpack content!");
                            for (int j = 0; j < 9; j++) {
                                ItemStack i = config.getItemStack(""+j);
                                inventory.setItem(j, i);
                        } else {
                            try {
                            } catch (IOException e) {
                                Bukkit.getLogger().info("Failed to create player save file!");
                        cancelled = false;
            if (cancelled) {
                player.sendMessage(ChatColor.BLUE + "You don't have a backpack!");
                return true;
        } else {
            sender.sendMessage(ChatColor.RED + "You need to be a player, to use this command!");

        return false;

    public void inventoryClose(InventoryCloseEvent event) { 
        File file = new File("plugins/ServerAssets/saves/" + player.getName() + ".yml");
        if (!(new File("plugins/ServerAssets/saves*" + player.getName() + ".yml").exists())) {
            try {
            } catch (IOException e) {
                Bukkit.getLogger().info("Failed to create user save file!");
        List<ItemStack> items = new ArrayList<ItemStack>();

        for (ItemStack i : inventory.getContents()) {

        save(items, file);

    public void save(List<ItemStack> items, File file) {
        int i = 0;
        for (ItemStack item : items) {
            config.set(""+i, item.serialize());
        try {
        } catch (IOException e) {
            Bukkit.getLogger().info("Failed to save config file while saving backpack content!");

When a null pointer exception is thrown when creating a new file object, that means one of the parameters is null, described in detail Here. What you're looking for on line 84 is

 File file = new File("plugins/ServerAssets/saves/" + event.getPlayer().getName() + ".yml");

Which according to the Bukkit API

final HumanEntity org.bukkit.event.inventory.InventoryCloseEvent.getPlayer ( )

will return the player associated with this event. The reason why the player object is null is that the event can be thrown before you execute a command, resulting in player never being set equal to anything.

  • \$\begingroup\$ So, i should change to event.getPlayer()? \$\endgroup\$
    – Bálint
    Jun 24 '15 at 12:37
  • \$\begingroup\$ Yes, you should insert the code I gave you above in place of line 84 \$\endgroup\$
    – jthort
    Jun 24 '15 at 12:37
  • \$\begingroup\$ Okay, i still have an error, but in the 105th row, wich is config.set(""+i, item.serialize()); Is there any problems with this? The apidocs aren't exist anymore, so i can't look up for answers. \$\endgroup\$
    – Bálint
    Jun 24 '15 at 12:39
  • \$\begingroup\$ I don't know what version of Bukkit you are using, but an API can be found here bukkit.luricos.de/api/1.7.9-R0.2/d2/d2e/… \$\endgroup\$
    – jthort
    Jun 24 '15 at 12:40
  • \$\begingroup\$ 1.8.7 R_01 from spigot BuildTools \$\endgroup\$
    – Bálint
    Jun 24 '15 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .