Java Bukkit inventory add生成nullpointerexception

Java Bukkit inventory add生成nullpointerexception,java,minecraft,bukkit,Java,Minecraft,Bukkit,我的问题存在于updateItem函数中,在该函数中,我希望通过设置函数将ItemStack添加到创建的库存“inv”中。但这会引发NullPointerException 这是我的密码: public class Shop { public static enum ShopMode { OPEN,CLOSED,FILLING } private int shopID; private Area area; private ShopMode mode;

我的问题存在于updateItem函数中,在该函数中,我希望通过设置函数将ItemStack添加到创建的库存“inv”中。但这会引发NullPointerException

这是我的密码:

public class Shop {

    public static enum ShopMode { OPEN,CLOSED,FILLING }

    private int shopID;
    private Area area;
    private ShopMode mode;
    private boolean disabled;
    private ArrayList<ItemStack> items = new ArrayList<ItemStack>();
    private Inventory inv;
    private Connection conn = null;
    private PreparedStatement ps = null;
    private Statement st = null;
    private ResultSet rs = null;
    private String name;

    public Shop(int no) {
        shopID = no;
        updateItems();
        setup();
    }

    private void setup() {
        inv = Bukkit.createInventory(null, 36, "Shop "+name);
        updateItems();
    }
    private void updateItems() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/odd","odd","odd");
            String sql = "SELECT * FROM `items` WHERE shopid = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, shopID);
            rs = ps.executeQuery();

            while (rs.next()) {
                String name = "";
                if (rs.getString("name") != "") {
                    name = rs.getString("name");
                    System.out.println("Name: "+name);
                }
                Material material = Material.valueOf(rs.getString("type"));
                int damage = rs.getInt("damage");
                System.out.println("Damage: "+damage);
                int count = rs.getInt("count");
                System.out.println("Count: "+count);
                //String[] ecs = null;
                if (rs.getString("enchantment") != "") {
                    //ecs = rs.getString("enchantment").split(";");
                }
                ItemStack is = new ItemStack(material,count,(byte) damage);
                //System.out.println(ecs.length);
                System.out.println("Enchants: "+rs.getString("enchantment"));
                /*if (ecs.length >= 1){
                    for (String enn : ecs) {
                        String[] enc = enn.split(enn);
                        is.addEnchantment(Enchantment.getByName(enc[0]),Integer.parseInt(enc[1]));
                    }
                }*/
                ItemMeta im = is.getItemMeta();
                if (rs.getString("lore") != "") {
                    List<String> list = new ArrayList<String>();
                    String[] t = rs.getString("lore").split(";");
                    for (int i = 0;i<t.length;i++) {
                        list.add(t[i]);
                    }
                    if (rs.getDouble("sell") <= 0) { 
                        list.add("Sorry, you can't sell this material to me."); 
                    } else { 
                        list.add("Sell: "+rs.getDouble("sell"));
                    }
                    if (rs.getDouble("buy") <= 0) { 
                        list.add("Sorry, you can't buy this item from me."); 
                    } else {
                        list.add("Buy: "+rs.getDouble("buy"));
                    }
                    im.setLore(list);
                    System.out.println("Lore: "+list.toString());
                }
                im.setDisplayName(name);
                System.out.println("DisplayName: "+name);
                is.setItemMeta(im);
                System.out.println("ItemStack: "+is.toString());
                inv.addItem(is);  // Line 176
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally { close(); }
    }    
}
这是我的错误日志:

[00:32:42 INFO]: Name: LOG
[00:32:42 INFO]: Damage: 2
[00:32:42 INFO]: Count: 1
[00:32:42 INFO]: Enchants:
[00:32:42 INFO]: Lore: [, Sorry, you can't sell this material to me., Sorry, you
 can't buy this item from me.]
[00:32:42 INFO]: DisplayName: LOG
[00:32:42 INFO]: ItemStack: ItemStack{LOG x 1, UNSPECIFIC_META:{meta-type=UNSPEC
IFIC, display-name=LOG, lore=[, Sorry, you can't sell this material to me., Sorr
y, you can't buy this item from me.]}}
[00:32:42 WARN]: java.lang.NullPointerException
[00:32:42 WARN]:        at no.jovang.odd.Shop.updateItems(Shop.java:176)
[00:32:42 WARN]:        at no.jovang.odd.Shop.<init>(Shop.java:39)
[00:32:42 WARN]:        at no.jovang.odd.manager.ShopManager.loadShops(ShopManag
er.java:106)
[00:32:42 WARN]:        at no.jovang.odd.manager.ShopManager.setup(ShopManager.j
ava:118)
[00:32:42 WARN]:        at no.jovang.odd.Odd$Startup.run(Odd.java:120)
[00:32:42 WARN]:        at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftTask.ru
n(CraftTask.java:71)
[00:32:42 WARN]:        at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftSchedul
er.mainThreadHeartbeat(CraftScheduler.java:350)
[00:32:42 WARN]:        at net.minecraft.server.v1_8_R1.MinecraftServer.z(Minecr
aftServer.java:709)
[00:32:42 WARN]:        at net.minecraft.server.v1_8_R1.DedicatedServer.z(Dedica
tedServer.java:316)
[00:32:42 WARN]:        at net.minecraft.server.v1_8_R1.MinecraftServer.y(Minecr
aftServer.java:634)
[00:32:42 WARN]:        at net.minecraft.server.v1_8_R1.MinecraftServer.run(Mine
craftServer.java:537)
[00:32:42 WARN]:        at java.lang.Thread.run(Unknown Source)
[00:32:42 INFO]: Name: LOG
[00:32:42 INFO]: Damage: 2
[00:32:42 INFO]: Count: 1
[00:32:42 INFO]: Enchants:
[00:32:42 INFO]: Lore: [, Sorry, you can't sell this material to me., Sorry, you
 can't buy this item from me.]
[00:32:42 INFO]: DisplayName: LOG
[00:32:42 INFO]: ItemStack: ItemStack{LOG x 1, UNSPECIFIC_META:{meta-type=UNSPEC
IFIC, display-name=LOG, lore=[, Sorry, you can't sell this material to me., Sorr
y, you can't buy this item from me.]}}

在你的商店里

public Shop(int no) {
   shopID = no;
   updateItems();
   setup();
}
您应该删除对updateItems的调用。这是因为需要首先调用setup方法来初始化inv成员变量。另外,由于安装程序调用updateItems本身,因此不需要在构造函数中调用updateItems

如果进行了此更改,那么当代码到达此行时

inv.addItem(is);    
inv将不会为null,这是您看到异常的原因。

当您尝试对值为null的对象调用方法时,会抛出一个。错误日志显示错误出现在第176行,即这一行:

inv.addItem(is);
正在引发,因为inv等于null

当代码到达inv.addItemis时,inv没有值,它为null,因此编译器不知道调用什么。addItemis打开,导致插件抛出

您可以在设置函数中初始化inv,因此,不要使用:

public Shop(int no){
    shopID = no; //this works fine
    updateItems(); //this causes the NPE - inv is not yet initialized
    setup(); //inv is initialized here, so it is now safe to call updateItems()
}
您应该切换位置updateItems和setup,并使用:

public Shop(int no){
    shopID = no; //this works fine
    setup(); //inv is initialized here, so it is now safe to call updateItems()
    updateItems(); //it is safe to call this because 'inv' was just initialized
}
此外,您的设置方法似乎在初始化inv后调用updateItems,因此您可以将设置缩短为:

最后,您的代码应该是这样的:

public class Shop {

    public static enum ShopMode { OPEN, CLOSED, FILLING }

    //...

    public Shop(int no){
        shopID = no;
        setup();
    }

    private void setup(){
        inv = Bukkit.createInventory(null, 36, "Shop "+name);
        updateItems();
    }

    private void updateItems(){
        //...
    }
}
public class Shop {

    public static enum ShopMode { OPEN, CLOSED, FILLING }

    //...

    public Shop(int no){
        shopID = no;
        setup();
    }

    private void setup(){
        inv = Bukkit.createInventory(null, 36, "Shop "+name);
        updateItems();
    }

    private void updateItems(){
        //...
    }
}