1

Here is my class where I've written the one liner code to link the fxml file to the project I'm working on:

package application;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;


public class towerOfHanoi extends Application implements Initializable 
{
    public static void main(String[] args) 
    {
        launch(args);
    }
    public void start(Stage primaryStage) throws Exception 
    {       
        try
        {
            Parent rootContainer = FXMLLoader.load(getClass().getResource("/application/userInterface.fxml"));
        Scene s=new Scene(rootContainer);
        primaryStage.setScene(s);
        //primaryStage.setTitle("Towers Of Hanoi");
        primaryStage.show();
        }
        catch(IOException e)
        {               
        //          e.printStackTrace();
        }
}

@Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
    }
}

Any time I try to run this in eclipse, nothing happens. The square to terminate is red as if something is happening, but the UI I created in scenebuilder doesn't show.

****Update: This is the complete stack trace when I uncomment that piece of code:

Mar 14, 2017 4:01:49 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.102 by JavaFX runtime of version 8.0.101
javafx.fxml.LoadException: No controller specified.
/F:/2nd%20Year%20College%20Stuff/Semester%202/Event-driven%20Programming/2nd%20Year%20Workspace/CA%202/bin/application/userInterface.fxml:44

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557)
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.towerOfHanoi.start(towerOfHanoi.java:29)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)
13
  • 2
    Try uncommenting e.printStackTrace(); Commented Mar 14, 2017 at 15:30
  • 1
    Additionally, note that Initializable is only called for controller classes... you don't typically use it your Application subclass... Commented Mar 14, 2017 at 15:31
  • If I comment it out I get a very huge error. Commented Mar 14, 2017 at 15:47
  • I've updated the OP with the stack trace. Commented Mar 14, 2017 at 16:04
  • 5
    Don't vandalize your post. Commented Mar 14, 2017 at 16:34

1 Answer 1

4

As the error tells you : you need to associate a controller to the .fxml, like this :

FILE userInterface.fxml (you can have another container than a BorderPane but it will be the same, it has to be on the root parent)

<BorderPane fx:id="background" fx:controller="application.Controller">
  //Content ...
</BorderPane>

you can set the Controller into the fxml (see above) OR by SceneBuilder:

enter image description here


Also It will be clearer to separate the Launcher and the controller, like this :

FILE towerOfHanoi.java

public class towerOfHanoi extends Application{
    public static void main(String[] args){
        launch(args);
    }
    public void start(Stage primaryStage) throws Exception{       
        try{
            Parent rootContainer = FXMLLoader.load(getClass().getResource("/application/userInterface.fxml"));
            Scene s=new Scene(rootContainer);
            primaryStage.setScene(s);
            //primaryStage.setTitle("Towers Of Hanoi");
            primaryStage.show();
        }
        catch(IOException e){               
        //          e.printStackTrace();
        }
    }
}

FILE Controller.java

public class Controller implements Initializable {
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Then what do you need me to add to the OP to help find the cause or the error?
OK, so it turns out that now the OP includes the stack trace, this does answer the question. :).
@James_D he update with the stack trace after my answer, I've updated so

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.