Solved World is null

Discussion in 'Plugin Development' started by lycopersicon, Nov 25, 2015.

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

    lycopersicon

    So I have a plugin that I am working on. Part of this requires a world constant. In the main of my plugin I have

    Code:
     
    public class Data extends JavaPlugin {
        public static final ChatColor G = ChatColor.GREEN;
        public static final ChatColor R = ChatColor.RED;
        public HashMap<String, String> tpa = new HashMap<String, String>();
        public HashMap<String, String> tpCheck = new HashMap<String, String>();
        public HashMap<String, String> tpSpawn = new HashMap<String, String>();
        public final util.Chat CB = new util.Chat(this);
        public World w = Bukkit.getWorld(getConfig().getString("World")); 
    That last line is giving me issues. It throws an NPE whenever I start the server. Error is "Unknown Source".

    If I do /reload the plugin loads fine and behaves as expected, it is only when starting the server that this happens. Therefor ruling out anything in the configuration. My thought was that the world simply wasn't loaded when the plugin fired up. So in my plugin.yml I have it load POSTWORLD, this does not appear to solve it. I am at a loss. Any assistance would be greatly appreciated.
     
  2. Offline

    mcdorli

    Show us the config
     
  3. Offline

    lycopersicon

    As I stated, the config is fine. Upon reload once the server is up, everything works fine, no errors at all.

    but here ya go:

    Code:
    
    # Data store for The Vine
    # Version 1.0 by Tomato
    #Coming and Going
    joinPlayer: '&aHello again &b%p !'
    joinBroadcast: '&5[&a+&5]&b%p &ahas joined the server'
    firstJoinPlayer1: '&aHello and welcome &b%p'
    firstJoinPlayer2: '&aWe hope you have a wonderful time playing here! :D'
    firstJoinBroadcast: '&5[&a+&5]&aPlease welcome our newcomer &b%p'
    leaveBroadcast: '&5[&c-&5]&b%p &ahas left the server'
    # Server info
    info1: '&b======================&6-HAVOC-&b======================'
    info2: '&aHavoc is a perpetual development server'
    info3: '&aEver evolving, adapting and getting better.'
    info4: '&aCore elements of gameplay will remain the same,'
    info5: '&abut new ideas and mechanics are added all the time.'
    info6: '&aHavoc has minimal rules, just do not cheat and you will be fine.'
    info7: '&aFeel free to &bexplore, discover &aand &bmake your own adventure.'
    info8: '&aThose who do so, &bwill be rewarded...'
    info9: '&aCurrent staff: &5Admin:  &4killahtomato (Tomato)'
    info10: '&b======================&6-HAVOC-&b======================'
    #Server rules 
    rules1: '&b====================&6HAVOC&b-&5RULES&b===================='
    rules2: '&4DO NOT &aCheat'
    rules3: '&b- &4No &aadvantageous mods such as minimap'
    rules4: '&b- &4No &amodded/hacked clients'
    rules5: '&b- &4No &aXray anything, mod or texture pack'
    rules6: '&4DO NOT &aSpam'
    rules7: '&b- &4No &aadvertising other servers or services'
    rules8: '&b- &4No &arepeatedly using commands, msg, tpa, ect... '
    rules9: '&4*&aIf it seems sketchy &4it most likely is, &aask if you are unsure. '
    rules10: '&4*&aGriefing/raiding is allowed, &4prepare accordingly'
    # Spawn cords
    World: 'world'
    spawn1: ''
    spawn2: ''
    Spawn1X: 0
    Spawn1Y: 0
    Spawn1Z: 0
    Spawn2X: 0
    Spawn2Y: 0
    Spawn2Z: 0
    loc.W: 0
    loc.X: 0
    loc.Y: 0
    loc.Z: 0
    loc.Yaw: 0
    loc.Pitch: 0
    
     
  4. Offline

    mcdorli

    Maybe because when it first starts, it don't save the config.
     
  5. Offline

    lycopersicon

    It does, I can put that line elsewhere and it works fine. Once again, when you do /reload everything behaves accordingly. It is only when you start the server initially it throws the error. The plugin works just fine, it does exactly what it is designed to do. I just don't want it to have errors every time the server gets started, and then having do /reload, as that is ill advised.
     
  6. Offline

    Lolmewn

    Of course - getConfig() isn't initialized yet on class creation (which is when your variables get created, too). Move your assignments to onEnable and you're good.
     
  7. Offline

    lycopersicon

    How would I reference that from another class if it is located in onEnable ?
     
  8. Offline

    mcdorli

    It is not located in the onEnable, it is located in the JavaPlugin, and your main class extends it. You need to pass an instance of the main with a contructor.
     
  9. Offline

    lycopersicon

    That is where it is currently located yes, and it is apparently what is causing the problem. If I move it to onEnable, how would I then access it from another class, such as my listener class?
     
  10. Offline

    mcdorli

    Pass an instance of the main to the listener, please, read my messages.
     
  11. Offline

    lycopersicon

    This has been done, I am however stuck on how exactly to reference the world, that is now located in the onEnable. I have read your messages. Maybe I am just not explaining it correctly.

    At this point all I am trying to find out is how, to get stuff from onEnable to my listener class, if that in fact is what I need to do. I have the main instance in the listener already, that has been done before I even asked for help. But I can't take that and get anything from onEnable. If you can show me a way to do that, it would be appreciated.
     
    Last edited: Nov 25, 2015
  12. Offline

    lycopersicon

    Okay, this is getting me pretty much nowhere, I know what a constructor is. I cannot get what is inside onEnable, I could and did previously access my assignments from my main class by using the instance of my main class. But since those assignments are moved to inside onEnable, I have no access to them from my listener class.
     
  13. Offline

    teej107

    https://docs.oracle.com/javase/tutorial/reflect/member/field.html. That's a hint....
     
  14. Offline

    lycopersicon

    Yea I am not about to get into reflection for something that should be simple and is entirely working except for one error that can be solved (not neatly, but solved) without getting into all that. I'll handle it on my own.
     
  15. Offline

    mcdorli

    pass the world to the listener maybe
     
  16. Offline

    lycopersicon

    The issue is this; The assignments are fine, I can pass them to any class I want, that is not the issue. The issue is mainly, that because they are defined using the config and the config doesn't get registered until AFTER the assignments are. Just like @Lolmewn said. He mentioned putting them inside onEnable, but then I don't have access to them from EventHandler for my listener class. When events occur I pull from my main class. So I just have them defined inside the event now, which is how I had them before, it is just inefficient because I end up creating new instances of things EVERY TIME an event gets called rather than have it predefined and just draw from it. Whatever, just ran in a circle with no better solution.
     
  17. Offline

    teej107

    Oh I linked to the wrong tutorial. Either way, use fields!

    not the Field object from reflection

    EDIT: And also stop abusing static. Get in the habit of assigning fields a value in the constructor (or onEnable())
     
  18. Offline

    lycopersicon

    Is there a reason not to use static? Does it hinder anything or is it just poor form?
     
  19. Offline

    Scimiguy

  20. Offline

    lycopersicon

    Marking as solved

    In Main class:
    Code:
        public World loadWorld(){
            World world = Bukkit.getWorld(getConfig().getString("World"));
            return world;
        }
    
    Still in Main class, inside onEnable:
    Code:
    loadWorld();
    
    The config loads then, initializes loadWorld() and nothing is null and I can grab it for what I was trying to do.
     
Thread Status:
Not open for further replies.

Share This Page