0

So I have to build this JavaFX application really fast, my code compiles yet the GUI doesn't start and I get exceptions. The problem starts as soon as the FileChooser code is implemented.

public class Main extends Application {

    @FXML //  fx:id="openButton"
    private Button openButton; // Value injected by FXMLLoader

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("Plot.fxml"));

        openButton.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent arg0) {
                FileChooser fileChooser = new FileChooser();
                FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
                fileChooser.getExtensionFilters().add(extFilter);
                File file = fileChooser.showOpenDialog(primaryStage);
                System.out.println(file);
            }
        });

        primaryStage.setTitle("Plotter");
        primaryStage.setScene(new Scene(root, 1024 , 768));
        primaryStage.show();
    }


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

The FXML file is as such:

<BorderPane prefHeight="400.0" prefWidth="723.0" xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <HBox alignment="TOP_CENTER" prefHeight="50.0" spacing="10.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="openButton" mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Open Dataset" />
            <Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button" />
            <Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button" />
         </children>
         <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </padding>
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
      </HBox>
   </top>
</BorderPane>

I am completely novice to JavaFX. Any tips is appreciated. P.S. I am using the Gluon scene builder.

Thanks.

The Exceptions:

Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NullPointerException at sample.Main.start(Main.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) ... 1 more

Process finished with exit code 1

Jishan
  • 1,654
  • 4
  • 28
  • 62
  • Could you supply the specific error message(s) that you're seeing? – kondrak Feb 27 '16 at 00:47
  • @ChiefTwoPencils added. – Jishan Feb 27 '16 at 00:50
  • @jewelsea if you actually have a proper answer other than marking duplicate and pointing at NullPointers, please write one, else, excuse! I hate this when people don't have a proper answer and point at irrelevant stuff! Excuse the question and go somewhere else! – Jishan Feb 27 '16 at 00:54
  • @jewelsea Thanks a lot! Much appreciated! – Jishan Feb 27 '16 at 01:02

1 Answers1

0

Answer is to separate the Controller and Application class, so that you don't end up with two instances of the Application class. Otherwise one instance of the Application class will not have some members initialized and will end up throwing null pointer exceptions.

package plot;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;

import java.io.File;

public class Controller {

    @FXML //  fx:id="openButton"
    private Button openButton; // Value injected by FXMLLoader

    @FXML
    public void open(ActionEvent e) {
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
        fileChooser.getExtensionFilters().add(extFilter);
        File file = fileChooser.showOpenDialog(openButton.getScene().getWindow());
        System.out.println(file);
    }

}

package plot;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception{
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Plot.fxml"));
        Parent root = loader.load();

        stage.setTitle("Plotter");
        stage.setScene(new Scene(root, 1024 , 768));
        stage.show();
    }

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

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<BorderPane prefHeight="400.0" prefWidth="723.0" xmlns="http://javafx.com/javafx/null"
            xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="plot.Controller"
>
  <top>
    <HBox alignment="TOP_CENTER" prefHeight="50.0" spacing="10.0" BorderPane.alignment="CENTER">
      <children>
        <Button fx:id="openButton" mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Open Dataset" onAction="#open"/>
        <Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button"/>
        <Button mnemonicParsing="false" prefHeight="31.0" prefWidth="150.0" text="Button"/>
      </children>
      <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
      </padding>
      <BorderPane.margin>
        <Insets/>
      </BorderPane.margin>
    </HBox>
  </top>
</BorderPane>

See related (I advise the first link be studied to understand why this solution works).

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406