我目前正与其他几个人一起制作Bukkit插件,并且我正在尝试创建一个日志系统。我一直试图修复我破损的代码2天,但一无所获。它似乎陷入困境的部分是我获取插件的数据文件夹,所以我可以继续写入文件夹中的文件。
堆栈跟踪:
[18:41:07] [Server thread/ERROR]: Error occurred while enabling TownyEco v1.0-SNAPSHOT (Is it up to date?)
java.lang.ExceptionInInitializerError: null
at com.writedive.townyeco.TownyEco.onEnable(TownyEco.java:67) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.2.jar:git-Spigot-4bd94dc-fb414fe]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_161]
Caused by: java.lang.NullPointerException
at com.writedive.townyeco.messaging.TELogWriter.getRootFolder(TELogWriter.java:12) ~[?:?]
at com.writedive.townyeco.messaging.TELogWriter.<clinit>(TELogWriter.java:14) ~[?:?]
... 12 more
来自的错误:
private static TownyEco plugin;
private static String getRootFolder() {
return plugin.getDataFolder().getPath();
}
private static String PATH = getRootFolder() + File.separator + "logs";
在主类的内部,从onEnable引用的记录器:
TELogWriter.log("Initialized", TELogWriter.LogType.STARTUP);
我该怎么做才能解决这个问题?
主要班级:TownyEco.java
记录器类:TELogWriter.java
答案 0 :(得分:2)
插件永远不会被初始化,因此下面的代码会抛出NPE
private static String getRootFolder() {
return plugin.getDataFolder().getPath();
}
在TownyEco.java中初始化TELogWriter.plugin然后调用日志
TELogWriter.plugin = plugin;
TELogWriter.log("Initialized", TELogWriter.LogType.STARTUP);
答案 1 :(得分:0)
不熟悉您正在使用的API,但
A)如果getDataFolder()有时仅由于不一致而返回null
private static String getRootFolder() {
FolerObjectTypeOfTheAPI path;
if (plugin != null && (path = plugin.getDataFolder()) != null) {
return path.getPath();
}
return null; // or you can return "";
}
B)如果总是为null,那么“getDataFolder()”中指定的路径很可能不是文件系统上的正确路径