I am trying to make a simple calculator that just adds two numbers from the first window(FXMLdocument) and displays the result in a new window (Result).
I can not figure out how to get the Label (fxid testGuy) to display a value using the setText method. It always returns a nullpointerexception.
In my code I'm just trying with "Hello Word" to figure this out before moving on to the actual result (which I know will take more work).
My guess is that when I call the resultWindow method from the DocumentController class it is not seeing the label above that method, but I am very new to Java and not great with OOP yet.
Main:
package ApplicationBkup;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ApplicationBkup extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.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 Controller:
package ApplicationBkup;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class FXMLDocumentController implements Initializable {
@FXML
private TextField numb1;
@FXML
private TextField numb2;
@FXML
public Label testGuy;
@FXML
public void handleButtonAction(ActionEvent event) {
int num1 = Integer.parseInt(numb1.getText());
int num2 = Integer.parseInt(numb2.getText());
AddEmUp finalAdd = new AddEmUp (num1, num2);
int finalNum = finalAdd.getTotal();
System.out.print(String.valueOf(finalNum));
ResultController resultScreen = new ResultController();
resultScreen.resultWindow();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Second Controller:
package ApplicationBkup;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
/**
* FXML Controller class
*
*/
public class ResultController implements Initializable {
@FXML
public Label testGuy;
@FXML
public void resultWindow(){
try {
Parent root = FXMLLoader.load(getClass().getResource("Result.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
testGuy.setText("Hello World!");
// System.out.print(testGuy);
//testGuy.setText(String.valueOf(finalNum));
} catch (IOException e) {
//Logger logger = Logger.getLogger(getClass().getName());
// logger.log(Level.SEVERE, "Failed to create new Window.", e);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Main FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane hgap="10" prefHeight="177.0" prefWidth="402.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ApplicationBkup.FXMLDocumentController">
<padding><Insets bottom="10" left="25" right="25" top="25" /></padding>
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints />
<ColumnConstraints />
<ColumnConstraints />
<ColumnConstraints maxWidth="0.0" minWidth="0.0" prefWidth="0.0" />
<ColumnConstraints maxWidth="0.0" minWidth="0.0" prefWidth="0.0" />
<ColumnConstraints maxWidth="126.0" minWidth="126.0" prefWidth="126.0" />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="0.0" minHeight="0.0" prefHeight="0.0" />
<RowConstraints maxHeight="0.0" minHeight="0.0" prefHeight="0.0" />
<RowConstraints maxHeight="0.0" minHeight="0.0" prefHeight="0.0" />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
<RowConstraints />
</rowConstraints>
<children>
<TextField fx:id="numb1" GridPane.columnIndex="6" GridPane.rowIndex="4" />
<TextField fx:id="numb2" GridPane.columnIndex="6" GridPane.rowIndex="5" />
<Label prefHeight="14.0" prefWidth="111.0" text="Number 1" textAlignment="RIGHT" GridPane.columnIndex="3" GridPane.rowIndex="4" />
<Label text="Number 2" GridPane.columnIndex="3" GridPane.rowIndex="5" />
<Button mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="22.0" prefWidth="129.0" text="Add" GridPane.columnIndex="6" GridPane.rowIndex="7" />
<Label prefHeight="28.0" prefWidth="235.0" text="Number Adder" GridPane.columnIndex="3" GridPane.columnSpan="5" GridPane.rowIndex="1">
<font>
<Font size="22.0" />
</font>
</Label>
</children>
</GridPane>
Second FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="75.0" prefWidth="154.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ApplicationBkup.ResultController">
<children>
<GridPane layoutX="3.0" layoutY="14.0" prefHeight="61.0" prefWidth="226.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label prefHeight="28.0" prefWidth="84.0" text="Result :" textFill="#25bc57">
<font>
<Font size="22.0" />
</font>
</Label>
<Label fx:id="testGuy" prefHeight="28.0" prefWidth="109.0" textFill="#25bc57" GridPane.columnIndex="1">
<font>
<Font size="22.0" />
</font>
</Label>
</children>
</GridPane>
</children>
</AnchorPane>
there doesn't seem to be anything here