NPE In an Async repeating task(and other issues)

Discussion in 'Spigot Plugin Development' started by HeyAwesomePeople, Jun 27, 2015.

  1. Yes an NPE is giving me an issue. But I ain't no noob. I cannot figure this one out, and assume it's because my task is async.

    http://hastebin.com/ikudenirow.coffee

    In the code above, my method, randomReward, is supposed do some fancy randomization in an inventory. It is supposed to do the code every 1 tick, and then slow down once it gets to it's 40th run. This happens, but at about the 49th round I get an NPE on line 70.

    Code (Text):
    [18:23:25] [Craft Scheduler Thread - 3/WARN]: Exception in thread "Craft Scheduler Thread - 3"
    [18:23:25] [Craft Scheduler Thread - 3/WARN]: org.apache.commons.lang.UnhandledException: Plugin AzideaEvents v1.0 generated an exception while executing task 4
        at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_8_R2.inventory.CraftItemStack.removeEnchantment(CraftItemStack.java:262)
        at me.heyawesomepeople.AzideaEvents.events.Methods$2.run(Methods.java:129)
        at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java:71)
        at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
        ... 3 more
    I know what this means, but "i" cannot be null, because I check for it. Also, AzideaEvents.ench_blank cannot be null either because it's already been used without a problem. So thats weird there.

    But I have another issue which I find weird, threads are being created automatically somehow...

    http://hastebin.com/equlipuciz.css

    Notice how it steadily uses thread 0, then starts to make new ones, then evens out to thread 19. Is this the result of me calling sleeps?

    And this may not be the best way to do this. I just need the ability to slow the runnable down as it gets closer to the end(50 runs in my case)

    Any help would be great, thanks.
     
  2. I'm not to sure what the issue is, but you shouldn't be using Bukkits API in an async task.
     
    • Agree Agree x 2
  3. I know, and that might be a problem. Do you suggest another way on how to change the rate at which the task repeats? This is the best solution I have come up with, and it doesn't seem to be working so well.
     
  4. You could run some Sync tasks that use Bukkit's API from inside your async task.