1

Screenshot of errors

I have main class. It is located in package "view"

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    try {
       AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("start.fxml"));
        Scene scene = new Scene(root, 757, 562);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Course Work");
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

It successfuly load file start.fxml. Which is also located in package "view".

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="562.0" prefWidth="757.0" styleClass="anchor-pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Start">
   <children>
      <Button layoutX="48.0" layoutY="466.0" mnemonicParsing="false" onAction="#start" text="Ок">
         <font>
            <Font size="14.0" />
         </font>
      </Button>
   </children>
</AnchorPane>

Controller for this start.fxml is located in package "controller".

public class Start {
private Stage stage;

    @FXML
    public void start(ActionEvent event) throws Exception { 
       try {
            AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("sample.fxml"));
            Scene scene = new Scene(root, 757, 562);
            stage.setScene(scene);
            stage.setTitle("Course Work");
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In method "start" I want to create new stage using sample.fxml. However I had some problems with it. For example - java.lang.NullPointerException. How can I fit it?

choko
  • 113
  • 5

1 Answers1

-1

The NullPointerException in this case might be because JavaFX framework is not able to find the provides .fxml file. You should provide fully qualified name, example as follows:

public Node loadResource(String resource) {
    try {
        return FXMLLoader.load(getClass().getResource("/com/google/resources/" + resource));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

In above code .fxml files are under com.google.resources package

Vishrant
  • 15,456
  • 11
  • 71
  • 120