0

I am making a bukkit arena plugin, but my savearena method always returns null. I am using the Bukkit API.

The error:

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

My current code is:

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);
     }
    }
    }
}

I have made comments on the code above for the lines that appeared in the error. Any idea on how to sove this problem? Thanks for reading my post :)

user3091392
  • 37
  • 2
  • 5

1 Answers1

0

What's the "main" object used on line 165 ? I guess it is null so "getDataFolder()" call on it returns a NPE.

Fabien Thouraud
  • 909
  • 1
  • 11
  • 21
  • Are you sure "main" object isn't "null" at ArenaManager's instantiation ? – Fabien Thouraud Dec 11 '13 at 14:46
  • Created a simple method just to print main.getName() and main.toString(); and got an error as well, in the line where I check that. The error was a NullPointerException as well, maybe main is null after all. How can I fix this? By the way, thanks for the reply. – user3091392 Dec 11 '13 at 15:07
  • Nobody can found that for you. Check your code since the instantiation of the Main class object to the line throwing NPE. You can also debug your application with your IDE. – Fabien Thouraud Dec 11 '13 at 15:21