nullPointerException when setting to config

Discussion in 'Plugin Development' started by AcePilot10, Jun 5, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    AcePilot10

    So, I've been working on this joust plugin. It's a simple minigame in which players fight on horses. My problem is whenever I run my commands to create the arenas. I don't believe I have any problems inside the onCommand method, but the error is happening when I call the createArena method in my ArenaManager, which just calls the setArenaPath method in my ConfigManager. Here's the setArenaPath method code:
    Code:
    public void setArenaPath(String arena, Player player) {
            if(!arenaDoesExist(arena)) {
                config.set(arena + ".Spawn1.X", 0);
                config.set(arena + ".Spawn1.Y", 0);
                config.set(arena + ".Spawn1.Z", 0);
                config.set(arena + ".Spawn1.World", "world");
                config.set(arena + ".Spawn2.X", 0);
                config.set(arena + ".Spawn2.Y", 0);
                config.set(arena + ".Spawn2.Z", 0);
                config.set(arena + ".Spawn2.World", "world");
                config.set(arena + ".Spec.X", 0);
                config.set(arena + ".Spec.Y", 0);
                config.set(arena + ".Spec.Z", 0);
                config.set(arena + ".Spec.World", "world");
                main.saveConfig();
    Here I just set the values of the data to 0, and world, But you need to set the spawns via commands which also change the config. I can't figure out why it's returning null as I have saved the config in my onEnable, so the config should be accessible. Anyways, any help would be awesome! Thanks!
     
  2. Offline

    NathanWolf

    What is "main" and do you initialize it?

    I assume it's a reference to your main plugin instance- double-check that it's getting set before this is called, or look at the stack trace and find the line number of the NPE, if it's the "main.saveConfig();" line, then that's probably the issue.
     
  3. Offline

    Zombie_Striker

    @AcePilot10
    Is config null? Does areanaDoesExists return true? Is arena null? is the Player null? Is main Null?

    You have no null checks, so this was bound to happen.
     
  4. Offline

    AcePilot10

    @NathanWolf the main instance if initialized in onEnable.
    @Zombie_Striker I have put in many if statements, and the problem ended up being the ArenaManager class was null.
    Thank you so much for your help guys!
     
  5. Offline

    Monollyth

    You could use a try and catch statement when adding stuff/saving a config. I've done it in the past, and it works quite well for cleaner config use.
    Example:
    Code:
    try{
        config.set("Test", Integer.valueOf(0));
        saveConfig();
    }  catch(NullPointerException e){
        e.printStackTrace();
    }
     
Thread Status:
Not open for further replies.

Share This Page