0

This is my Main Class

package application;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("application/anwendung.fxml"));
            primaryStage.setTitle("Benutzerverwaltung");
            root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

This is my Controller class

package application;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.input.InputEvent;
import javafx.stage.Stage;

class AnwendungsController {

    @FXML
    public Button closeButton;


    @FXML
    public void handleCloseButtonAction(ActionEvent event) {
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }
}

And this is my fxml file

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="140.0" prefWidth="350.0"     xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1"     fx:controller="application.AnwendungsController">
  <children>
   <Button fx:id="closeButton" layoutX="18.0" layoutY="100.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" onMouseClicked="#onMouseClickedCancelBtn" prefHeight="26.0" prefWidth="77.0"  text="Abbrechen" />
   <Label layoutX="10.0" layoutY="27.0" prefHeight="27.0" prefWidth="342.0" text="Sie können das System nun verwenden" textAlignment="CENTER" textOverrun="CLIP">
     <font>
        <Font name="System Bold" size="18.0" />
     </font>
  </Label>
   </children>
  </AnchorPane>

How can I fix it? I try everything from here JavaFX "Location is required." even though it is in the same package

UPDATE:

java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
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.Main.start(Main.java:20)
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)
Community
  • 1
  • 1
yascod
  • 13
  • 1
  • 5
  • i dont understand what you mean – yascod Dec 01 '16 at 22:47
  • The resource being inside the package does not automatically mean it's added to the classpath when it's run. Eclipse and IntellIJ seem to have a tendency of not doing this... – fabian Dec 01 '16 at 23:03
  • what i must do so that its add to the classpath? @fabian – yascod Dec 01 '16 at 23:05
  • Depends on how you run your program. I'm pretty sure there are a few questions about this, at least for IntellIJ there is more than one... – fabian Dec 01 '16 at 23:08
  • I use Eclipse @fabian – yascod Dec 01 '16 at 23:11
  • What part of 'not using a null at whichever line of code threw the exception' don't you understand? I don't see how anybody can help you if you don't understand basics like this. – user207421 Dec 01 '16 at 23:24
  • @EJP: In this case the question seems to be more about the reason why `getResource` returns `null` than understanding what a NPE is. Nonetheless I'm pretty sure it should be a dupe of some question. E.g. http://stackoverflow.com/questions/2850674/where-to-put-a-textfile-i-want-to-use-in-eclipse and http://stackoverflow.com/questions/9867273/getresources-returns-null look promissing... – fabian Dec 01 '16 at 23:28

1 Answers1

3

Two problems exist:

1)The File path

2)The Controller was not set properly

Recommendation:

If you are using SceneBuilder, when you want to see what your controller might look like, you can go to View -> Show Sample Controller Skeleton.


  • Solution

Main class:

package application;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            System.out.println(getClass().getResource("anwendung.fxml").getPath());

            //other solution
            //FXMLLoader loader = new FXMLLoader(getClass().getResource("anwendung.fxml"));
            //Parent root = loader.load();

            //Keep in mind that you are calling a static method
            Parent root = FXMLLoader.load(getClass().getResource("anwendung.fxml"));

            primaryStage.setTitle("Benutzerverwaltung");
            root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

Controller:

package application;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;

public class AnwendungsController {

    @FXML
    private Button closeButton;

    @FXML
    void handleCloseButtonAction(ActionEvent event) {

    }

    @FXML
    void onMouseClickedCancelBtn(MouseEvent event) {

    }

}

FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="140.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AnwendungsController">
  <children>
   <Button fx:id="closeButton" layoutX="18.0" layoutY="100.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" onMouseClicked="#onMouseClickedCancelBtn" prefHeight="26.0" prefWidth="77.0" text="Abbrechen" />
   <Label layoutX="10.0" layoutY="27.0" prefHeight="27.0" prefWidth="342.0" text="Sie können das Systema nun verwenden" textAlignment="CENTER" textOverrun="CLIP">
     <font>
        <Font name="System Bold" size="18.0" />
     </font>
  </Label>
   </children>
  </AnchorPane>
Zephyr
  • 9,885
  • 4
  • 28
  • 63
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • 1. is exactly the same thing that i use and the two others dont work – yascod Dec 01 '16 at 22:45
  • @yascod Well in your question i see `Parent root = FXMLLoader.load(getClass().getResource("application/anwendung.fxml"));` i added all this 3 ways to try them and see which it works . The `Main.class` and `answendung.fxml` are in the same package? From where the null pointer is coming? Give a StackTrace in your question :) – GOXR3PLUS Dec 01 '16 at 22:48
  • @yascod Also when you want to send a message to someone in the community you have to use this pattern `@NameOfTheUser` , you can see it in the comment above. – GOXR3PLUS Dec 01 '16 at 22:50
  • @yascod You are ready . Also it is recommended to delete the comments that are not needed , so future users can see only what is useful :) – GOXR3PLUS Dec 01 '16 at 23:20