0

Background Info:

Java FXML instantiates the variable for you. It does not need to explicitly be done again or it will lose the reference that FXML created.

Annotation @FXML are only required on methods to ensure that it is being injected. Although it is good practice to put it above variables, it does not solve the problem

I am trying to change a label's text because I eventually want to display a specific error message for the user to read. At the moment I left the label with the text "label". When I run the program I noticed if i load a new scene and then click the button to change the label it work, but if I try to load the scene "next" and try to change the label's text i get a Null pointer error. So the line that comes right after Main.next() will give me the null pointer, but if i take it out and then click the button "ok" to then change the text of the label it works. The problem is i want it to display the different error messages by changing label and i want to display it as soon as the scene is loaded. Can anyone help me on this?

`<VBox alignment="CENTER" 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" 
fx:controller="sample.Controller">
<children>
<Label fx:id="lb2" alignment="CENTER" text="Label" />
<Button fx:id="ok" mnemonicParsing="false" onAction="#ok" text="Button" />
</children>
</VBox>`




<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" 
minWidth="-Infinity" prefHeight="300.0" prefWidth="600.0" 
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="sample.Controller">
<children>
  <Button fx:id="pushMe" layoutX="79.0" layoutY="80.0" 
mnemonicParsing="false" onAction="#pushMeAction" text="Push me" />
</children>
</Pane>



 package sample;

 import javafx.fxml.FXML;
 import javafx.scene.control.Button;
 import javafx.scene.control.Label;
 import javafx.scene.text.Text;
 import javafx.scene.text.TextFlow;
 import java.awt.*;
 import java.io.IOException;
 import java.util.Timer;
 import java.util.concurrent.TimeUnit;

 public class Controller
 {
    @FXML
    public Button pushMe;
    public Label lb2;


    public void pushMeAction() throws Exception
    {
        Text text1 = new Text("Hello World");

        Main.next();
        lb2.setText("Hello");

    }

    public void ok()
    {
        lb2.setText("Hello");
    }
 }



 public class Main extends Application {

    public static Stage primaryStage;

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

    public static void next() throws IOException
    {
        Pane menu = FXMLLoader.load(Main.class.getResource("next.fxml"));
        Scene myScene = new Scene(menu);
        primaryStage.setScene(myScene);

    }


    public static void main(String[] args) {
        launch(args);
    }
}
  • 1
    Because you never instantiate your Label lb2 in your Controller class. Just so you know, this has nothing to do with using JavaFX or not, this is basic Java. – Stultuske Mar 12 '19 at 06:41
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stultuske Mar 12 '19 at 06:43
  • @Stultuske When using FXML, the variable is created for you so if you were to instantiate it again it would not reference the one created by FXML. I appreciate the effort though. – Vicente Romero Mar 12 '19 at 17:16
  • Yes, but you didn't use the FXML annotation on that variable. only on the pushMe button. Your pushMe and your lb2 are also not in the same block in your configuration. It's difficult to see if they are the same file the way you posted your code, but chances are, you are trying to bind it with a wrong resource – Stultuske Mar 13 '19 at 07:01
  • FXML annotation is only for good practices it does not have to have it. Please if your not aware of what the issue is try not to confuse other people that are interested in the same question. – Vicente Romero Mar 13 '19 at 22:06
  • Indeed. I'm the one not understanding the concept of a NullPointerException. My bad. Indeed, my knowledge of JavaFX is limited, which is why I didn't post an answer, yet replied in comments. – Stultuske Mar 14 '19 at 08:01
  • The fact that you answered in comments doesn't make you knowledgeable about a subject. hahaha The concept of Null Pointer was never the issue. The question was conceptually why was is happening in regards to FXML. The fact that you don't know what FXML annotations are for nor did you know that FXML instantiates the variables is a clear indication that you have no experience or knowledge on the subject. SO "Indeed, you don't know anything about this subject. " But again I appreciate the effort. – Vicente Romero Mar 14 '19 at 20:58
  • ... yah. But, again: you only annotated one variable. for the variable that is annotated, it'll work. for the variable you didn't annotate, a bit less so. I may indeed not have much experience with JavaFX, but at least I wasn't to lazy to google. Check the accepted answer on this thread: https://stackoverflow.com/questions/30210170/is-fxml-needed-for-every-declaration I might indeed not be the most knowledgeable (never claimed I was), but I'm not the one stuck with your problems. – Stultuske Mar 15 '19 at 06:54
  • It was an issue, but I got around it by coding it by hand instead of using fxml, but I'm still very curious about why this doesn't work. If you would have read my post, you would have seen that the code does work it does not need to be annotated FXML its only considered to be good practice. It is only necessary on a method. I was trying to say there is one line in the code that causes and error BUT that same line placed somewhere else does not cause the error. So the FXML is not the problem. I love how you assume peoples work ethic based on a question you didn't even fully read. – Vicente Romero Mar 15 '19 at 15:17

1 Answers1

0

You should write it like this:

@FXML
public Button pushMe;

@FXML
public Label lb2;

@FXML
public Button ok;

@FMXL
public void pushMeAction(ActionEvent event) throws Exception
{
Text text1 = new Text("Hello World");

Main.next();
lb2.setText("Hello");

}

@FXML
public void ok(ActionEvent event)
{
    lb2.setText("Hello");
}
Raw
  • 461
  • 7
  • 15