0

Why in this simple code, i'm get a exception? I'm just trying to add a button to the panel, I can't find the problem by searching, the feeling that either I am the only one, or I do not understand some fundamental things. This code performs the only function, it just adds a button to the form built in fxml

public class Wtf extends Application {

@FXML
    public AnchorPane noteList;

    @Override
    public void start(Stage primaryStage) throws Exception {
        final FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("wtf.fxml"));
        final Parent root = loader.load();
        Button button = new Button();
        noteList.getChildren().add((button));


        Scene scene = new Scene(root);
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

FXML:

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

<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<Pane fx:controller="Wtf" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane fx:id="noteList" layoutX="119.0" layoutY="68.0" prefHeight="292.0" prefWidth="288.0">
         <children>
            <VBox layoutX="44.0" layoutY="14.0" prefHeight="207.0" prefWidth="190.0" />
         </children>
      </AnchorPane>
   </children>
</Pane>

StackTrace:

Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: 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$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at tests.Wtf.start(Wtf.java:25)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$3(WinApplication.java:177)
    ... 1 more
  • 2
    Don’t use the `Application` class as the controller class. – James_D Aug 29 '20 at 21:45
  • Please tell me how to fix this? – Kirill Orlov Aug 29 '20 at 21:53
  • Use a different class for the controller. You can define an `initialize()` method in it which will be called automatically after the fxml has been parsed. – James_D Aug 29 '20 at 21:57
  • Sorry, but I don't understand what you mean. I started to study javafx recently, I don't understand well yet. I just need to add the code to the ready-made fxml form by clicking the button. I have an application that has a place where I have to add a new to the by clicking a button. Can I load fxml into the controller incorrectly? How do I determine Initialize? If it doesn't bother you, tell me where to read. I read the instructions of oracle, unfortunately I did not find an answer to my question there. – Kirill Orlov Aug 29 '20 at 22:13
  • 2
    I don’t know how to make it simpler. I assume you know what a class is, and what a method is. Create a new class and use it for the controller, and define a method called `initialize()` where you can do whatever you need to do once the FXML has been loaded, accessing any elements with `fx:id` attributes just like you are trying to do here. – James_D Aug 29 '20 at 22:16

1 Answers1

1

This works for me, using the FXML file to place the button

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();

}


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

And the FXML file

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
  <Button layoutX="274.0" layoutY="175.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>

EDIT

If you want to use the FX Id then make a seperate Class since its a better practice. Like this...

public class Main extends Application {


@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("ButtonTestClass.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}


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

New class used for the button

public class ButtonTestClass {

@FXML
private Button buttonId;

}

And the FXML File...

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" 
 xmlns="http://javafx.com/javafx/11.0.1" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="sample.ButtonTestClass">
<children>
    <Button fx:id="buttonId" layoutX="331.0" layoutY="131.0" 
 mnemonicParsing="false" text="Button" />
    </children>
 </AnchorPane>
HugoS
  • 105
  • 11
  • But I cannot then use this button anyway through fx: id in the code, for example, I cannot call the onAction method on its fx: id, I get exactly the same error – Kirill Orlov Aug 29 '20 at 21:55
  • Just made an edit based on what you wanted. – HugoS Aug 29 '20 at 22:09
  • I already have an application form in fxml. And it runs in the controller. It has an AnchorPane, to which you need to add a button, by clicking on which elements will be added to the same AnchorPane in its -> . And this happens in the stage, which is started in the controller. I don't understand how separate classes will help me. I can link to a full-fledged application and indicate the location where this error is so that it is clear why I need fx: id – Kirill Orlov Aug 29 '20 at 22:24
  • https://gist.github.com/setaniel/1b652e8ab6023373dbccef299c862575 App.java file on line 58 gets an error the first time – Kirill Orlov Aug 29 '20 at 22:29
  • Note that this is __not__ a personal helpdesk! And if you don't understand the answers/comments, you must go back to to learning the basics until you do, no way around ;) – kleopatra Aug 30 '20 at 09:13