Question about NPE on getBlock

Discussion in 'Plugin Development' started by Bradley Hilton, Aug 14, 2012.

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

    Bradley Hilton

    I've had this problem for a while now, I've tried to catch the NPE and handle it but for some reason I've yet to be able to successfully do it. Here's the error that's happening and here's a link to the source file of the SlotData.

    Code:
    [SEVERE] Error occurred while enabling CasinoSlots v2.4.7.1 (Is it up to date?)
    java.lang.NullPointerException
        at org.bukkit.Location.getBlock(Location.java:82)
        at com.craftyn.casinoslots.slot.SlotData.getBlocks(SlotData.java:162)
        at com.craftyn.casinoslots.slot.SlotData.loadSlot(SlotData.java:140)
        at com.craftyn.casinoslots.slot.SlotData.loadSlots(SlotData.java:89)
        at com.craftyn.casinoslots.util.ConfigData.load(ConfigData.java:53)
        at com.craftyn.casinoslots.CasinoSlots.onEnable(CasinoSlots.java:80)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:365)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:383)
        at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:374)
        at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:356)
        at net.minecraft.server.MinecraftServer.i(MinecraftServer.java:306)
        at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:285)
        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:225)
        at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:168)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:388)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    What exactly does this NPE mean? Does it mean that the block that I'm trying to get the location of is null? If so, how would I go about catching that? Because I've tried:

    Code:
    if (loc.getBlock() == null) {}
    But for some reason that's not catching it and it's still throwing an NPE at the console. Suggestions?
     
  2. Offline

    Courier

    It means the world associated with that location is null.
     
  3. Offline

    Bradley Hilton

    How can a world be null if it's the main one and is loaded? Also, not to doubt you, but do you happen to have a link to somewhere that states that or is that personal experience?
     
  4. Offline

    Courier

  5. Offline

    Bradley Hilton

  6. Offline

    Jnorr44

    you could just do World world = loc.getWorld();
     
  7. Offline

    Bradley Hilton

    I do, see here
     
  8. Offline

    Jnorr44

    Just add null checks throughout your code... Probably a good idea anyways. That will fix lots of possible bugs.
     
  9. Offline

    Courier

    That would stop your error, but the real source of your problem is a few lines up(line 156), when you get the world from your config. Print out the name it loads from your config, and make sure it is what you expect. Bukkit.getWorld() is returning null for you, probably because that world doesn't exist, or isn't loaded.

    He is creating the Location objects with a world whose name is specified in his config, he already has a reference to the world, except it is null.

    I think you need to use Bukkit.createWorld(...); (or getServer().createWorld(...)).

    This loads the world if it exists, and tells the server how to create new chunks of the world.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 6, 2018
  10. Offline

    Bradley Hilton

    Okay, so it's probably that it's not loaded. Have any suggestions as to how to get the world loaded or check if that world is loaded?

    Thanks, realize now that I did a small error in my coding elsewhere. It's returning null because that's the value of it... /facepalm. Thanks for your help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 6, 2018
Thread Status:
Not open for further replies.

Share This Page