A main fxml is present.When clicked on center button center fxml file is loaded. On clicking the button,the data in text field is accessed and set text label.the setText label is not working.It cannot be accessed.Null pointer exception is given.
Test.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author root
*/
public class Test extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<BorderPane prefHeight="374.0" prefWidth="534.0" style="-fx-background-color: grey;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.mainController">
<top>
<HBox prefHeight="51.0" prefWidth="200.0" style="-fx-background-color: white;" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
<children>
<TextField fx:id="tf" prefHeight="52.0" prefWidth="318.0" />
<Button mnemonicParsing="false" onMouseClicked="#handleButtonAction" prefHeight="53.0" prefWidth="52.0" text="Button" />
</children>
</HBox>
</top>
<center>
<BorderPane fx:id="borderpane1" BorderPane.alignment="CENTER">
<center>
<HBox fx:id="hb" prefHeight="100.0" prefWidth="200.0" style="-fx-background-color: white;" />
</center>
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
</BorderPane>
</center>
<left>
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<Button mnemonicParsing="false" onMouseClicked="#handleButtonAction1" prefHeight="43.0" prefWidth="99.0" text="Center" />
</children>
</VBox>
</left>
</BorderPane>
center.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="336.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.mainController">
<children>
<Label fx:id="label" prefHeight="65.0" prefWidth="229.0" text="Label">
<font>
<Font size="56.0" />
</font>
</Label>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
maincontroller.java
package test;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
public class mainController implements Initializable {
public StringProperty nlt(){
return label.textProperty();
}
@FXML
Label label;
@FXML
TextField tf;
@FXML
BorderPane borderpane1;
@FXML
HBox hb;
@FXML
private void handleButtonAction(MouseEvent event)throws IOException {
System.out.println("You clicked me!");
FXMLLoader Home;
Home = new FXMLLoader(getClass().getResource("center.fxml"));
hb.getChildren();
String s=tf.getText();
System.out.println(s);
label.setText(s);
}
@FXML
private void handleButtonAction1(MouseEvent event) throws IOException {
FXMLLoader Home;
Home = new FXMLLoader(getClass().getResource("center.fxml"));
//borderpane1.setCenter(Home);
hb.getChildren().add((Node)Home.load());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Cannot change label. Error=>Null Pointer EXception,java.lang.RuntimeException: java.lang.reflect.InvocationTargetException.....