好的,所以我一直在使用Eclipse为Minecraft制作Bukkit插件。出于某种原因,我得到了许多看似不合理的NullPointerExceptions。我发现当您尝试使用值为null的varriable类型的值和函数时,会发生NullPointerExceptions。我在我的代码中找不到它,所以我想知道是否有其他人可以找到它。
错误:
20:16:58 [SEVERE] java.lang.NullPointerException
20:16:58 [SEVERE] at com.tommy3244.plugins.MakeYourOwnBlocks.MakeYourOwnBl
ocks.onDisable(MakeYourOwnBlocks.java:73)
20:16:58 [SEVERE] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
in.java:219)
20:16:58 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin
(JavaPluginLoader.java:481)
20:16:58 [SEVERE] at org.bukkit.plugin.SimplePluginManager.disablePlugin(S
implePluginManager.java:400)
20:16:58 [SEVERE] at org.bukkit.plugin.SimplePluginManager.disablePlugins(
SimplePluginManager.java:393)
20:16:58 [SEVERE] at org.bukkit.plugin.SimplePluginManager.clearPlugins(Si
mplePluginManager.java:434)
20:16:58 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.reload(Craf
tServer.java:563)
20:16:58 [SEVERE] at org.bukkit.Bukkit.reload(Bukkit.java:184)
20:16:58 [SEVERE] at org.bukkit.command.defaults.ReloadCommand.execute(Rel
oadCommand.java:23)
20:16:58 [SEVERE] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
mmandMap.java:186)
20:16:58 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCom
mand(CraftServer.java:514)
20:16:58 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchSer
verCommand(CraftServer.java:506)
20:16:58 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedServer.al(Dedica
tedServer.java:260)
20:16:58 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedServer.r(Dedicat
edServer.java:225)
20:16:58 [SEVERE] at net.minecraft.server.v1_4_6.MinecraftServer.q(Minecra
ftServer.java:494)
20:16:58 [SEVERE] at net.minecraft.server.v1_4_6.MinecraftServer.run(Minec
raftServer.java:427)
20:16:58 [SEVERE] at net.minecraft.server.v1_4_6.ThreadServerApplication.r
un(SourceFile:849)
这是我的实际插件代码:
package com.tommy3244.plugins.MakeYourOwnBlocks;
import java.io.File;
import java.util.*;
import org.bukkit.block.*;
import org.bukkit.command.*;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.*;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
public class MakeYourOwnBlocks extends JavaPlugin implements Listener
{
public Map<NewItem, List<ItemStack>> items;
public File configfile;
@SuppressWarnings("unchecked")
public void onEnable()
{
try
{
Map<List<Map<String, Object>>, NewItem> smap;
smap = SaveLoadAPI.load(this.getDataFolder().getPath()+"\\Items.data");
for(List<Map<String, Object>> maps : smap.keySet())
{
NewItem item = smap.get(maps);
List<ItemStack> stacks = new ArrayList<ItemStack>();
for(Map<String, Object> map : maps)
{
stacks.add(ItemStack.deserialize(map));
}
items.put(item, stacks);
}
}
catch(Exception e)
{
items = new HashMap<NewItem, List<ItemStack>>();
}
try
{
this.getDataFolder().mkdirs();
}
catch(Exception e)
{
}
configfile = new File(getDataFolder(), "config.yml");
if(!configfile.exists())
{
saveResource("config.yml", false);
}
try
{
getConfig().load(configfile);
}
catch(Exception error)
{
getLogger().severe("Could not load config!");
error.printStackTrace();
}
getServer().getPluginManager().registerEvents(this, this);
getLogger().info("MakeYourOwnBlocks successfully enabled!");
}
public void onDisable()
{
try
{
Map<List<Map<String, Object>>, NewItem> smap = new HashMap<List<Map<String, Object>>, NewItem>();
for(NewItem item : items.keySet())
{
List<ItemStack> stacks = items.get(item);
List<Map<String, Object>> stacks2 = new ArrayList<Map<String, Object>>();
for(ItemStack stack : stacks)
{
stacks2.add(stack.serialize());
}
smap.put(stacks2, item);
}
SaveLoadAPI.save(smap, this.getDataFolder().getPath()+"\\Items.data");
}
catch (Exception e)
{
getLogger().severe("Was unable to save the items file to the default path of: "+this.getDataFolder().getPath()+"\\Items.data");
e.printStackTrace();
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onItemStackRightClick(PlayerInteractEvent e)
{
Player player = e.getPlayer();
//getLogger().info("Player "+player.getName()+" has right clicked.");
for(NewItem item : items.keySet())
{
for(ItemStack curstack : items.get(item))
{
ItemMeta lore = ((ItemStack)curstack).getItemMeta();
List<String> lore2 = player.getItemInHand().getItemMeta().getLore();
if(lore == null || lore2 == null)
{
getLogger().info("Pointer null!");
if(lore == null)
{
getLogger().info("Pointer 1 null");
}
else
{
getLogger().info("Pointer 2 null");
}
}
if(lore2.equals(lore))
{
getLogger().info(player.getDisplayName()+" right clicked with the same item!");
boolean right = e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK;
boolean block = e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK;
boolean worked;
if(right)
{
worked = item.onRightClick(player, right, block);
}
else
{
worked = item.onLeftClick(player, right, block);
}
if(!worked)
{
player.sendMessage("Script fault! Error raised.");
}
}
}
}
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(sender instanceof Player)
{
}
else
{
sender.sendMessage("You must be a player to perform this command!");
return false;
}
if(cmd.getName().equalsIgnoreCase("makeitem"))
{
if(args.length < 4)
{
return false;
}
String name = args[0];
String type = args[1];
String data = args[2];
String damage = args[3];
try
{
int dmg = Integer.parseInt(damage);
byte dta = (byte)Integer.parseInt(data);
int id = Integer.parseInt(type);
if(getConfig().contains("items."+name))
{
ConfigurationSection section = getConfig();
List<List<String>> lines = new ArrayList<List<String>>();
lines.add(null);
lines.add(null);
if(getConfig().contains("items."+name+".onrightclick"))
{
List<String> cur = getConfig().getStringList("items."+name+".onrightclick");
lines.set(0, cur);
}
else
{
lines.set(0, null);
}
if(getConfig().contains("items."+name+".onleftclick"))
{
List<String> cur = getConfig().getStringList("items."+name+".onleftclick");
lines.set(1, cur);
}
else
{
lines.set(1, null);
}
List<ItemStack> stacks = new ArrayList<ItemStack>();
ItemStack stack = new ItemStack(id, 1, dta);
ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(name);
List<String> lore;
if(getConfig().contains("items."+name+".description"))
{
lore = getConfig().getStringList("items."+name+".description");
}
else
{
lore = new ArrayList<String>();
}
lore.add(name);
NewItem item = new NewItem(lore, name, id, dta, lines, dmg, false, section);
meta.setLore(lore);
stack.setItemMeta(meta);
getLogger().info(stack.toString());
stacks.add(stack);
if(items == null)
{
getLogger().info("unused");
}
items.put(item, stacks);
Player player = (Player)sender;
player.getLocation().getWorld().dropItem(player.getLocation(), stack);
player.sendMessage("Here you go!");
}
else
{
sender.sendMessage("No configuration section at \""+"items."+name+"\"");
return true;
}
}
catch(Exception error)
{
//sender.sendMessage("Error: "+error.getLocalizedMessage());
//System.out.println(error);
//throw error;
//makeitem test 272 0 0
error.printStackTrace();
return true;
}
sender.sendMessage("Error: No command specified");
}
return true;
}
}
更新:
nakib回答了我的问题,错误已修复!非常感谢!
答案 0 :(得分:2)
在第73行,你有
for(NewItem item : items.keySet())
调用items
时,属性onDisable()
为空。
要解决此问题,您应该初始化声明中的items
public Map<NewItem, List<ItemStack>> items = new HashMap<NewItem, List<ItemStack>>();
答案 1 :(得分:1)
items
变量必须为null
。您可以在课程顶部修复它:
public Map<NewItem, List<ItemStack>> items = new HashMap<NewItem, List<ItemStack>>();
你看起来有几个区域你认为它已经被初始化了,所以我要么在类的顶部,要么在你的构造函数中(它还不存在:)。
答案 2 :(得分:1)
从它的外观来看,我打赌items
(第73行)为空,你要求它的值。添加一些内容以确保如果第28行的for块没有至少运行一次,则不会调用该方法。
答案 3 :(得分:0)
堆栈跟踪告诉您NullPointerException
来自:
20:16:58 [SEVERE] java.lang.NullPointerException
20:16:58 [SEVERE] at com.tommy3244.plugins.MakeYourOwnBlocks.MakeYourOwnBl
ocks.onDisable(MakeYourOwnBlocks.java:73) <-- HELPFUL ADVICE FROM STACK TRACE
68 public void onDisable()
69 {
70 try
71 {
72 Map<List<Map<String, Object>>, NewItem> smap = new HashMap<List<Map<String, Object>>, NewItem>();
73 for(NewItem item : items.keySet()) <--- THIS LINE IS PUKING, SO items is probably null!
了解如何解释这些消息以及使用调试器在现代IDE中逐步执行代码至关重要。如果没有这些工具,您就没有足够的能力来导航大型代码库。
我给出的另一条建议是smap
不是面向对象的。某种类型的封装在这里会有所帮助,因此您不需要Map
List
Map
的{{1}} .....我们还在谈论什么?