I Read Anime
I Read Anime

Reputation: 29

Argument mismatch; method cannot be converted to TimerTask

I keep getting this kind of error:

error: no suitable method found for schedule(soccer,int)

method Timer.schedule(TimerTask,long) is not applicable

(argument mismatch; soccer cannot be converted to TimerTask)

method Timer.schedule(TimerTask,Date) is not applicable

(argument mismatch; soccer cannot be converted to TimerTask)

import java.util.*;
public class Balls{
    private static int time = 1;

    public static void main(String[] args){
        int a=0,b=0,c=0,d=0,e=0;
        volley one = new volley();
        beach two = new beach();
        soccer three = new soccer();
        basket four = new basket();
        pokemon five = new pokemon();

        Timer myTime1 = new Timer();
        Timer myTime2 = new Timer();
        Timer myTime3 = new Timer();
        Timer myTime4 = new Timer();
        Timer myTime5 = new Timer();

        for(int x=0; x<20000; x+=5000){
            if(x==5000){
                myTime1.schedule(one,(x-1000));
                myTime1.schedule(two,(x+500));
                myTime1.schedule(three,(x-500));
                myTime1.schedule(four,(x+1000));
                myTime1.schedule(five,(x-500));
            }
            else if(x==10000){
                myTime1.schedule(one,(x+500));
                myTime1.schedule(two,(x-1000));
                myTime1.schedule(three,(x+1000));
                myTime1.schedule(four,(x-500));
                myTime1.schedule(five,(x+500));
            }
            else if(x==15000){
                myTime1.schedule(one,(x-500));
                myTime1.schedule(two,(x+1000));
                myTime1.schedule(three,(x+500));
                myTime1.schedule(four,(x+500));
                myTime1.schedule(five,(x+1000));
            }
            else if(x==20000){
                myTime1.schedule(one,(x+1000));
                myTime1.schedule(two,(x-500));
                myTime1.schedule(three,(x-1000));
                myTime1.schedule(four,(x-1000));
                myTime1.schedule(five,(x-1000));
            }
            else{
                break;
            }
        }    
    }
}

class volley extends TimerTask{
public void run(){
    System.out.println("Volley ball");
}
}
class beach extends TimerTask{
public void run(){
    System.out.println("Beach ball");
}
}
class soccer{
public void run(){
    System.out.println("Soccer ball");
}
}
class basket{
public void run(){
    System.out.println("Basket ball");
}
}
class pokemon{
public void run(){
    System.out.println("Pokemon Ball");
}
}

Sorry for poor post, first time doing it.

Upvotes: 2

Views: 1308

Answers (2)

Minzkraut
Minzkraut

Reputation: 2187

You are creating a new object of the type soccer
soccer three = new soccer();
and then you try to pass this object of type "soccer" as the first argument of the timers schedule function.
But the schedule function expects the first argument to be of type TimerTask.

What you need to do is

  • make your soccer class extend from TimerTask

  • @Override the run() function.

  • Creat a new object of type TimerTask (because your class extends TimerTask, see examples below)

For example:

class soccer extends TimerTask{
    @Override
    public void run(){
        System.out.println("Soccer ball");
    }
}

Then you just have to create the object like this:

TimerTask three = new soccer();

Extending from TimerTask also gives you the cancel() and scheduledExecutionTime() functions.
The latter returns the scheduled execution time of the most recent actual execution of this task.
cancel() is pretty self-explanatory, it cancels the timer task.

So you could call for example:

three.cancel()    

or

long execTime = three.scheduledExecutionTime()

Upvotes: 1

Adomas
Adomas

Reputation: 506

Class soccerneeds to extend TimerTask in order to be scheduled by Timer, like this:

class soccer extends TimerTask {
   @Override
   public void run(){
       System.out.println("Soccer ball");
   }
}

The same applies for basket and pokemon classes.

Actually, your code doesn't compile because those classes are not instances of TimerTask and you try to use them in myTime1.schedule

Upvotes: 1

Related Questions