在方法中创建文件

时间:2013-12-11 14:25:32

标签: java nullpointerexception bukkit

我正在制作一个bukkit竞技场插件,但我的savearena方法总是返回null。我正在使用Bukkit API。

错误:

Caused by: java.lang.NullPointerException
    at me.Livid_C0ffee.Sandfall.Arena_Manager.ArenaManager.saveArena(ArenaMa
nager.java:165) ~[?:?]
    at me.Livid_C0ffee.Sandfall.Arena_Manager.ArenaManager.createArena(Arena
 Manager.java:62) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
ftbukkit.jar:git-Bukkit-1.6.4-R2.0-37-g4857595-b2951jnks]
    ... 13 more

我目前的代码是:

private Main main;
public ArenaManager(Main main)
{
    this.main = main;
}

int arenanum = 0;    
List<Arena> arenas = new ArrayList<Arena>();    
 List<Integer> arenasid = new ArrayList<Integer>();

public Arena createArena(String world, Location loc1, Location loc2, Location spawnpoint) throws IOException{
    int number = arenanum + 1;
    arenanum++;
    Arena a = new Arena(number, loc1, loc2, world, spawnpoint);
    int id = a.getId();
    arenas.add(a);
    arenasid.add(a.getId());
    saveArena(id); //line 62
    return null;
}


public Arena getArena(int id) throws IOException{
    if(id != 0){
        if(arenasid.contains(id)){
           for(Arena a: arenas){
                if(a.getId() == id){
                    return a;
                }
           }
            arenasid.remove(id);
            arenas.remove(a);
        }else{
         File arena = new File(main.getDataFolder() + File.separator + "Arenas" + File.separator + String.valueOf(id) + ".yml");
            if(arena.exists()){
                FileConfiguration con = YamlConfiguration.loadConfiguration(arena);
                //loc1
                World loc1w = Bukkit.getWorld(con.getString("Point_1_location.world"));
                Double loc1x = con.getDouble("Point_1_location.x");
                Double loc1y = con.getDouble("Point_1_location.y");
                Double loc1z = con.getDouble("Point_1_location.z");
                Location loc1 = new Location(loc1w,loc1x,loc1y,loc1z);
                World loc2w = Bukkit.getWorld(con.getString("Point_2_location.world"));
                Double loc2x = con.getDouble("Point_2_location.x");
                Double loc2y = con.getDouble("Point_2_location.y");
                Double loc2z = con.getDouble("Point_2_location.z");
                Location loc2 = new Location(loc2w,loc2x,loc2y,loc2z);
                String world = con.getString("World");
                World sw = Bukkit.getWorld(con.getString("Spawnpoint_location.world"));
                Double sx = con.getDouble("Spawnpoint_location.x");
                Double sy = con.getDouble("Spawnpoint_location.y");
                Double sz = con.getDouble("Spawnpoint_location.z");
                Location spawnp = new Location(sw,sx,sy,sz);
                Arena a = new Arena(id, loc1, loc2, world, spawnp);
                return a;
            }
        }


        }
    return null;

}

public void saveArena(int id) throws IOException{
    if(id != 0){
  Arena a = getArena(id);
        if(a != null){
            String idd = String.valueOf(a.getId());
    File arena = new File(main.getDataFolder() + File.separator + "Arenas" + File.separator + idd + ".yml"); //line 165
     if(!arena.exists()){
       FileConfiguration con = YamlConfiguration.loadConfiguration(arena);
       con.set("ID", a.getId());
       con.set("Point_1_location.x", a.getLoc1().getX());
       con.set("Point_1_location.y", a.getLoc1().getY());
       con.set("Point_1_location.z", a.getLoc1().getZ());
       con.set("Point_1_location.world", a.getLoc1().getWorld().getName());
         con.set("Point_2_location.x", a.getLoc2().getX());
         con.set("Point_2_location.y", a.getLoc2().getY());
         con.set("Point_2_location.z", a.getLoc2().getZ());
         con.set("Point_2_location.world", a.getLoc2().getWorld().getName());
       con.set("World", a.getLoc1().getWorld().getName());
         con.set("Spawnpoint_location.x", a.getSpawnpoint().getX());
         con.set("Spawnpoint_location.y", a.getSpawnpoint().getY());
         con.set("Spawnpoint_location.z", a.getSpawnpoint().getZ());
         con.set("Spawnpoint_location.world", a.getSpawnpoint().getWorld().getName());
         con.save(arena);
     }
    }
    }
}

我已经对错误中出现的行的上述代码发表了评论。关于如何解决这个问题的任何想法?感谢您阅读我的帖子:)

1 个答案:

答案 0 :(得分:0)

第165行使用的“主要”对象是什么?我猜它是null所以“getDataFolder()”调用就会返回一个NPE。