2013-12-11 21 views
0

我在製作一個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); 
    } 
    } 
    } 
} 

我已經對以上對出現錯誤的行代碼中的註釋。任何想法如何解決這個問題?感謝您閱讀我的文章:)

+0

不,您的'saveArena'不會返回'null',因爲它是'void'方法。 – kviiri

+1

'main.getDataFolder()'?主要是空? –

+0

更新後的文章,主要是我的主類。 – user3091392

回答

0

線165上使用的「主要」對象是什麼?我想它是空的,所以「getDataFolder()」調用它返回一個NPE。

+0

編輯代碼:) – user3091392

+0

你確定ArenaManager實例中的「main」對象不是「null」嗎? –

+0

創建一個簡單的方法來打印main.getName()和main.toString();並得到了一個錯誤,在我檢查的行中。該錯誤也是一個NullPointerException,也許main是空的。我怎樣才能解決這個問題?順便說一句,謝謝你的回覆。 – user3091392