我是编程bukkit插件的新手,所以如果答案显而易见,请放轻松。我正在尝试创建第二个配置文件来存储位置和其他数据,但是当我运行我的插件时,我不断获得NullPointerException
。
这是我的代码:
package blah.blah.blah.XD;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public class KingdomLocationsFile extends JavaPlugin implements Listener{
public Kingdoms plugin;
public KingdomLocationsFile(Kingdoms plugin) {
this.plugin = plugin;
}
private File KingdomLocationsFile;
private FileConfiguration KingdomLocations;
public void reloadKingdomLocations() {
if (KingdomLocationsFile == null) {
KingdomLocationsFile = new File(getDataFolder(), "KingdomLocations.yml");
}
KingdomLocations = YamlConfiguration.loadConfiguration(KingdomLocationsFile);
// Look for defaults in the jar
InputStream defConfigStream = plugin.getResource("KingdomLocations.yml");
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfig
Stream);
KingdomLocations.setDefaults(defConfig);
}
}
public FileConfiguration getKingdomLocations() {
if (KingdomLocations == null) {
this.reloadKingdomLocations();
}
return KingdomLocations;
}
public void saveKingdomLocations() {
if (KingdomLocations == null || KingdomLocationsFile == null) {
return;
}
try {
getKingdomLocations().save(KingdomLocationsFile);
} catch (IOException ex) {
this.getLogger().log(Level.SEVERE, "Could not save config to " + KingdomLoc
ationsFile, ex);
}
}
public boolean force = false;
public List<Location> getLocationsList(final String path) {
final List<Location> List = new ArrayList<Location>();
if (this.KingdomLocations.contains(path) && this.KingdomLocations.isConfigu
rationSection(path)) {
final Set<String> keys = this.KingdomLocations.getConfigurationSect
ion(path).getKeys(false);
if (keys.size() > 0) {
final Location[] key = (Location[]) keys.toArray();
for (final Location element : key) {
List.add(this.getKingdomLocation(path +"." +(Locati
on) element));
}
}
}
return List;
}
private Location getKingdomLocation(String path) {
Location loc = (Location) this.KingdomLocations.get(path);
return loc;
}
}
......这是错误:
C:\Users\Mark\Desktop\Plugin Test Server>java -Xms1024M -Xmx1024M -jar craftbukk
it-1.5.1-R0.1-20130326.111705-23.jar -o true
229 recipes
27 achievements
16:06:39 [INFO] Starting minecraft server version 1.5.1
16:06:39 [INFO] Loading properties
16:06:39 [INFO] Default game type: SURVIVAL
16:06:39 [INFO] Generating keypair
16:06:39 [INFO] Starting Minecraft server on *:25565
16:06:39 [INFO] This server is running CraftBukkit version git-Bukkit-1.4.7-R1.0
-75-g7f25632-b2717jnks (MC: 1.5.1) (Implementing API version 1.5.1-R0.1-SNAPSHOT
)
16:06:39 [INFO] [Kingdoms] Loading Kingdoms v1.0
16:06:39 [INFO] Preparing level "world"
16:06:39 [INFO] Preparing start region for level 0 (Seed: 3764664766498884770)
16:06:40 [INFO] Preparing start region for level 1 (Seed: 3764664766498884770)
16:06:40 [INFO] Preparing spawn area: 20%
16:06:41 [INFO] Preparing start region for level 2 (Seed: 3764664766498884770)
16:06:41 [INFO] [Kingdoms] Enabling Kingdoms v1.0
16:06:41 [SEVERE] Error occurred while enabling Kingdoms v1.0 (Is it up to date?
)
java.lang.NullPointerException
at com.treeclimber17.kingdoms.KingdomLocationsFile.reloadKingdomLocation
s(KingdomLocationsFile.java:35)
at com.treeclimber17.kingdoms.KingdomLocationsFile.getKingdomLocations(K
ingdomLocationsFile.java:44)
at com.treeclimber17.kingdoms.Kingdoms.onEnable(Kingdoms.java:14) [THIS PROBABLY DOESN't MATTER...]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
.java:457)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
r.java:381)
at org.bukkit.craftbukkit.v1_5_R2.CraftServer.loadPlugin(CraftServer.jav
a:282)
at org.bukkit.craftbukkit.v1_5_R2.CraftServer.enablePlugins(CraftServer.
java:264)
at net.minecraft.server.v1_5_R2.MinecraftServer.j(MinecraftServer.java:3
01)
at net.minecraft.server.v1_5_R2.MinecraftServer.e(MinecraftServer.java:2
80)
at net.minecraft.server.v1_5_R2.MinecraftServer.a(MinecraftServer.java:2
40)
at net.minecraft.server.v1_5_R2.DedicatedServer.init(DedicatedServer.jav
a:150)
at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java
:379)
at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:5
73)
16:06:41 [INFO] Server permissions file permissions.yml is empty, ignoring it
16:06:41 [INFO] Done (1.885s)! For help, type "help" or "?"
答案 0 :(得分:0)
你应该在bukkit自己的支持页面上保留与bukkit相关的问题(因为它是一个API),可以在这里找到:http://forums.bukkit.org/forums/plugin-development.5/
话虽如此,尝试使用try-catch来查看语句是否仍然为空:
KingdomLocations = YamlConfiguration.loadConfiguration(KingdomLocationsFile);
类似的东西:
try {
KingdomLocations = YamlConfiguration.loadConfiguration(KingdomLocationsFile);
} catch (Exception e) {
System.out.println("Exception caught in method");
System.out.println(e.printStackTrace());
}
有点徒手画,但没有手头的api /行号,很难准确找出问题所在。