Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8063483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:03:19+00:00 2026-06-05T11:03:19+00:00

I am trying to get this program to cancel itself after running 3 times,

  • 0

I am trying to get this program to cancel itself after running 3 times, but I keep getting a “The local variable taskId may not have been initialized” error. For one is this dangerous. Two can I still properly compile it and run the program despite getting compiler errors? Lastly how would I fix this? Thanks!

final int taskId = plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    int count = 0;
    @Override
    public void run() {
        ((LivingEntity) e.getEntity()).damage(2);
        count++;
        if (count > 2) {
            Bukkit.getScheduler().cancelTask(taskId);
        }
    }

}, 60L);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:03:21+00:00Added an answer on June 5, 2026 at 11:03 am

    It is possible you will try to use the taskId before it is initialised.

    This will not work in your case, because it is not access only in cancelTask(taskId) but when the anonymous class is constructed. i.e. before it has been set.

    A work around is to use a int[] task instead but this is risky.

    A simpler work around is to throw a RuntimeException such as IllegalStateException or one of your own as this will stop the task repeating.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get this simple program to work on windows, but it crashes:
I've been trying to get this program to work, but when I try to
Im doing C homework and trying to get this program to run, but I
I have been trying to get this program to work but so far having
i'm trying to get this program to break down a user defined amount of
Im trying to get this working but for some reason it's just not right.
Been trying to get this up and running for a while now. Basically i
I'm trying to get this simple windows dialog to spawn but as soon as
I am running windows 7 and I am trying to get this IDE to
I've been trying to Run a simple program on NetBeans but just after I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.