Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA incompatible types: Object cannot be converted to my type

Tags:

java

javafx

I'm trying to make changes to my GUI in JavaFX by doing the work on a separate thread and returning the object required. However, after doing the work and task.setOnSucceeded() is triggered I attempt to retrieve the created object and get the error "incompatible types: Object cannot be converted to type VideoScrollPane".

I think this has something to do with raw types as it's within the listener this is happening but after looking around I couldn't find the suggestions I was looking for.

Any light that can be shed would be much appreciated.

Task task = new Task<VideoScrollPane>() {
    VideoScrollPane vsp;
    @Override protected VideoScrollPane call() {
        try {
            System.out.print("thread...");

            ExecutorService executor = Executors.newCachedThreadPool();
            Future<VideoScrollPane> future = executor.submit(new Callable<VideoScrollPane>() {
                @Override public VideoScrollPane call() {
                    return new VideoScrollPane(mediaview, vboxCentre, username, project);
                }
            });

            vsp = future.get();
        } catch(Exception exception) { System.out.println(exception.getMessage()); }

        return vsp;
    }
};
new Thread(task).start();

task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
    @Override public void handle(WorkerStateEvent t) {
        System.out.println("complete");

        try {

            //where the problem occurs
            VideoScrollPane v = task.get();     

        } catch(Exception exception) { System.out.println(exception.getMessage()); }
    }
});
like image 680
macourtney7 Avatar asked Mar 12 '15 18:03

macourtney7


People also ask

How to solve incompatible type error in Java?

Swapping the inappropriate constructor call with an instance of the correct type solves the issue, as shown in Fig. 6(b). Fig. 6 also serves as an example to show how the incompatible types error is, in fact, a generalization of the method X in class Y cannot be applied to given types error explored in [4].

How do you fix a string that Cannot be converted to int?

The most direct solution to convert a Java string to an integer is to use the parseInt method of the Integer class: int i = Integer. parseInt(myString); parseInt converts the String to an int , and throws a NumberFormatException if the string can't be converted to an int type.


2 Answers

This is because the task.get() is returning a value of type Object, but you're trying to assign it to v, which is a VideoScrollPane. You can prevent the error by doing a cast, like so

VideoScrollPane v = (VideoScrollPane)task.get();

Be warned, if task.get() returns something that isn't a VideoScrollPane, you'll get a ClassCastException.

If you want to prevent the problem entirely however, consider fixing the declaration of task, by including a type for the generic parameter. You could change it to,

Task<VideoScrollPane> task = new Task<VideoScrollPane>() {

This way, task.get() will now return a VideoScollPane, and you won't need a cast.

like image 80
Minty Fresh Avatar answered Sep 28 '22 10:09

Minty Fresh


You have declared your Task incorrectly. You need

Task<VideoScrollPane> task = new Task<VideoScrollPane>() { ... }
like image 40
James_D Avatar answered Sep 28 '22 08:09

James_D