help.

Discussion in 'Plugin Development' started by boss86741, Feb 15, 2013.

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

    boss86741

    My class:
    Code:
    package com.boss86741.plugins.allday;
     
    import java.util.List;
     
    import org.bukkit.World;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class AllDay extends JavaPlugin {
        public List<World> adanWorlds;
        long repeat;
       
        public void timechk() {
            for (World w : adanWorlds) {
                Long now = w.getTime();
                Long dawn = getConfig().getLong("worlds." + w.getName() + ".dawn");
                Long dusk = getConfig().getLong("worlds." + w.getName() + ".dusk");
                if (now < dawn || now > dusk) {
                    w.setTime(dawn);
                }
            }
        }
       
        @Override
        public void onEnable() {
            getLogger().info("Enabled!");
            getLogger().info("It will now always be day.");
            getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    timechk();
                }
            }, 60L, repeat);
        }
    }
    
    My error:
    Code:
    07:59:23 [WARNING] [AllDay] Task #16 for AllDay v1.0 generated an exception
    java.lang.NullPointerException
        at com.boss86741.plugins.allday.AllDay.timechk(AllDay.java:13)
        at com.boss86741.plugins.allday.AllDay$1.run(AllDay.java:29)
        at org.bukkit.craftbukkit.v1_4_6.scheduler.CraftTask.run(CraftTask.java:53)
        at org.bukkit.craftbukkit.v1_4_6.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
        at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:530)
        at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
     
  2. Offline

    xmarinusx

    boss86741
    Can you post the lines: 13 and 29?

    boss86741 Ah I see, on line 13 you're looping through a List what's null. That causes a nullPointerExeption. I would recommend first checking if the list is not null and than loop through it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  3. Offline

    hapm

    Simply initialize your adanWorlds member in onEnable:

    Code:
    adanWorlds = new ArrayList<World>();
    You don't need to check for adanWorlds being null, as it will always have a reference when your plugin is enabled.

    Btw, i would suggest you to save the world names instead of the world objects in your List and retrieve the worlds in the loop, by using:
    Code:
    World w = getServer().getWorld(worldName)
    That'll avoid memory leaks and that you are working with an outdated World object. Be sure to check if w is null and use continue; to skip the current iteration if the world with the given name can't be found.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  4. Offline

    boss86741

    Code:
    17:05:53 [WARNING] [AllDay] Task #2 for AllDay v1.0 generated an exception
    java.lang.NullPointerException
        at com.boss86741.plugins.allday.AllDay.timechk(AllDay.java:13)
        at com.boss86741.plugins.allday.AllDay$1.run(AllDay.java:29)
        at org.bukkit.craftbukkit.v1_4_R1.scheduler.CraftTask.run(CraftTask.java:53)
        at org.bukkit.craftbukkit.v1_4_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:344)
        at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:530)
        at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    
    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  5. Offline

    teunie75

    boss86741 It still generates that NPE, did you checked it before using?
     
  6. Offline

    boss86741

    yes. I checked.
     
Thread Status:
Not open for further replies.

Share This Page