我试图将键和值放在另一个类的HashMap中。 NPE不应该发生。我可以正确设置实例设置,因为我可以毫无问题地访问RingCreation中的方法,但是当我尝试将对象放入Main中的HashMap时,它将返回空指针错误,但是当我生成HashMap的调试消息时,它检测到它不为空。我不想通过发布“又一个NPE”日志来造成麻烦。通常,我可以修复大多数出现的错误。
主班
HashMap<Integer,ItemStack> ringHolder = new HashMap<Integer,ItemStack>();
private RingCreation createRing;
private static Main instance;
public Main() {
this.createRing = new RingCreation();
}
@Override
public void onEnable() {
if(ringHolder.isEmpty()) System.out.println("ringholder is empty");
instance = this;
createRing.CreateLifeRing();
createRing.CreateRegenerationRing();
createRing.CreateStarterRing();
}
public static Main getInstance() {
return instance;
}
}
RingCreation类:
private RingOfLife ringLife;
private StarterRing startRing;
private RingOfRegeneration regenRing;
private Main main = Main.getInstance();
public RingCreation() {
this.ringLife = new RingOfLife();
this.startRing = new StarterRing();
this.regenRing = new RingOfRegeneration();
}
ItemStack ringOfLife;
ItemMeta ringOfLifeMeta;
ItemStack starterRing;
ItemMeta starterRingMeta;
ItemStack ringOfRegeneration;
ItemMeta ringOfRegenerationMeta;
public void CreateLifeRing() {
ringOfLife = new ItemStack(Material.RECORD_4);
ringOfLifeMeta = ringOfLife.getItemMeta();
ringOfLifeMeta.setDisplayName(ChatColor.GRAY+"Ring of"+ChatColor.GREEN+" Life");
ringOfLifeMeta.addItemFlags(ItemFlag.values());
ringOfLife.setItemMeta(ringOfLifeMeta);
System.out.println("createlifering working");
int loc = 2;
main.ringHolder.put(loc, ringOfLife);
}
public void CreateStarterRing() {
starterRing = new ItemStack(Material.RECORD_11);
starterRingMeta = starterRing.getItemMeta();
starterRingMeta.setDisplayName(ChatColor.GRAY+"Starter Ring");
//removes the music disc lore V
starterRingMeta.addItemFlags(ItemFlag.values());
starterRing.setItemMeta(starterRingMeta);
int loc = 1;
main.ringHolder.put(loc, starterRing);
}
public void CreateRegenerationRing() {
ringOfRegeneration = new ItemStack(Material.RECORD_5);
ringOfRegenerationMeta = ringOfRegeneration.getItemMeta();
ringOfRegenerationMeta.setDisplayName(ChatColor.GRAY+"Ring of"+ChatColor.GREEN+" Regeneration");
ringOfRegenerationMeta.addItemFlags(ItemFlag.values());
ringOfRegeneration.setItemMeta(ringOfRegenerationMeta);
int loc = 3;
main.ringHolder.put(loc, ringOfRegeneration);
}
错误:
[14:17:37 ERROR]: Error occurred while enabling VelocusRings v1.0 (Is it up to date?)
java.lang.NullPointerException: null
at me.redshadus.velocus.RingCreation.CreateLifeRing(RingCreation.java:44) ~[?:?]
at me.redshadus.velocus.Main.onEnable(Main.java:33) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.2.jar:git-Spigot-eb3d921-2b93d83]
at java.lang.Thread.run(Unknown Source) [?:?]
第33行:
createRing.CreateLifeRing();
RingCreation第44行:
main.ringHolder.put(loc, starterRing);
控制台还输出:createlifering工作
答案 0 :(得分:1)
问题是您永远不会初始化instance
的{{1}}:
更改:
Main
到
public static Main getInstance() {
return instance;
}
这是“延迟加载”单例的典型模式。
另外,您应该将构造函数设为私有public static Main getInstance() {
if(instance == null) {
instance = new Main();
}
return instance;
}
,因为我希望应该只有private Main()
的一个实例。
答案 1 :(得分:0)
我认为您忘记创建一个Main
实例。