Phouthasak Douanglee
Phouthasak Douanglee

Reputation: 11

Not a sub class error

import javafx.application.Application;    
import javafx.scene.Scene;    
import javafx.scene.control.Button;   
import javafx.scene.control.TextField;  
import javafx.scene.layout.BorderPane;  
import javafx.stage.Stage;

public class Calc {  

  public class Gui extends Application{  
    @Override  
    public void start(Stage stage){  
        TextField mainText = new TextField();  

        BorderPane pane = new BorderPane();
        pane.setTop(mainText);

        Scene scene = new Scene(pane);
        stage.setTitle("Calculator");
        stage.setScene(scene);
        stage.show();
    }
 }

    public static void main(String args[]){
        Application.launch(args);
    }
} 

When I try to exececute the program I get this error:

Exception in thread "main" java.lang.RuntimeException: Error: class Calc is not a subclass of javafx.application.Application at javafx.application.Application.launch(Application.java:211) at Calc.main(Calc.java:56)

I don't understand how it's not a subclass when the scope of the import is over the Calc class so that means the sub classes of those should be able to use them imports.

Upvotes: 1

Views: 7719

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503519

I don't understand how it's not a subclass when the scope of the import is over the Calc class so that means the sub classes of those should be able to use them imports.

Subclassing and imports are entirely different things. It's complaining that your Calc class is declared as:

public class Calc

rather than

public class Calc extends Application

Now your Gui class is a subclass of Application - but as you can see from the error message, JavaFX isn't trying to launch Gui, it's trying to launch Calc.

Personally I'd suggest getting rid of the extra class, and just having Calc extend Application, with both the main method (for launching) and the start method. Alternatively, you could specify the Gui class explicitly:

public static void main(String args[]){
    Application.launch(Gui.class, args);
}

... but then you'd also want to make the Gui class static, so that it would genuinely have a parameterless constructor. (Currently you need an instance of Calc in order to create an instance of Gui, because it's an inner class.)

Upvotes: 6

Related Questions