I'm having issues using label.setText(String)
. I have set fx:id
's for the labels I am trying to use setText()
on and linked my Controller to my .fxml file.
I initially had this issue trying to populate the labels with values from my SQLite DB when the scene loaded and decided to see if it works when the scene is loaded via Button(ActionEvent)
as I was unsure if my EventListener
was working.
I can print the values of the variables to console fine but not label.setText()
. I have looked at other submitted questions like this one and this one also but no further forward.
Any help is appreciated!
Options.java
package PengVapour;
import Controllers.OptionsController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class Options extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage optionsStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("../FXML/Options.fxml"));
Parent root = (Parent)loader.load();
OptionsController opCont = (OptionsController)loader.getController();
optionsStage.addEventHandler(WindowEvent.WINDOW_SHOWN, event -> opCont.handleSceneShownEvent());
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("../CSS/PengVapour.css").toExternalForm());
optionsStage.setTitle("Peng Vapour - Application Options");
optionsStage.setScene(scene);
optionsStage.show();
optionsStage.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
OptionsController.java (I have removed a lot of code that is not required for this question)
package Controllers;
import Database.OptionsDB;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class OptionsController implements Initializable {
@FXML MenuItem fileExit;
@FXML Button mainMenu;
@FXML TextField concentrateThreshold;
@FXML TextField vgThreshold;
@FXML TextField pgThreshold;
@FXML TextField nicotineThreshold;
@FXML TextField cleanerThreshold;
@FXML public static Label currentConcentrateThresholdLabel;
@FXML public static Label currentVGThresholdLabel;
@FXML public static Label currentPGThresholdLabel;
@FXML public static Label currentNicotineThresholdLabel;
@FXML public static Label currentCleanerThresholdLabel;
@FXML CheckBox confirm;
@FXML Button submit;
@FXML Button testB;
public static double concentrateUpdateValue;
public static double vgUpdateValue;
public static double pgUpdateValue;
public static double nicotineUpdateValue;
public static double cleanerUpdateValue;
public boolean confirmed = false;
@Override
public void initialize(URL location, ResourceBundle resources) {}
@FXML
public void handleSceneShownEventTest(ActionEvent e) throws InvocationTargetException {
OptionsDB.showCurrentThresholds();
}
OptionsDB.java (I have removed a lot of code that is not required for this question)
package Database;
import Controllers.OptionsController;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
public class OptionsDB {
public static String concentrateCurrentThreshold;
public static String vgCurrentThreshold;
public static String pgCurrentThreshold;
public static String nicotineCurrentThreshold;
public static String cleanerCurrentThreshold;
public static void main() {
connect();
}
public static Connection connect() {
String url = "jdbc:sqlite:C:\\Users\\PengStation420\\IdeaProjects\\PengVapour\\PVIM.sqlite";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public static void showCurrentThresholds() throws InvocationTargetException {
String sqlShowCurrentThresholds = "SELECT * FROM Options";
try (Connection conn = connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlShowCurrentThresholds)){
while (rs.next()) {
concentrateCurrentThreshold = String.valueOf(rs.getDouble("ConcentrateThreshold"));
vgCurrentThreshold = String.valueOf(rs.getDouble("VGThreshold"));
pgCurrentThreshold = String.valueOf(rs.getDouble("PGThreshold"));
nicotineCurrentThreshold = String.valueOf(rs.getDouble("NicotineThreshold"));
cleanerCurrentThreshold = String.valueOf(rs.getDouble("CleanerThreshold"));
System.out.println(concentrateCurrentThreshold);
System.out.println(vgCurrentThreshold);
System.out.println(pgCurrentThreshold);
System.out.println(nicotineCurrentThreshold);
System.out.println(cleanerCurrentThreshold);
OptionsController.currentConcentrateThresholdLabel.setText(concentrateCurrentThreshold);
OptionsController.currentVGThresholdLabel.setText(vgCurrentThreshold);
OptionsController.currentPGThresholdLabel.setText(pgCurrentThreshold);
OptionsController.currentNicotineThresholdLabel.setText(nicotineCurrentThreshold);
OptionsController.currentCleanerThresholdLabel.setText(cleanerCurrentThreshold);
}
System.out.println();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
Options.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="vBox" alignment="CENTER" minHeight="450" minWidth="600" prefHeight="450.0" prefWidth="600.0" stylesheets="@../CSS/PengVapour.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.OptionsController">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="fileExit" mnemonicParsing="false" onAction="#menuBarFileExit" text="Exit" />
</items>
</Menu>
</menus>
</MenuBar>
<Label fx:id="head" alignment="CENTER" prefHeight="58.0" prefWidth="600.0" stylesheets="@../CSS/PengVapour.css" text="Options" textAlignment="CENTER" />
<AnchorPane prefHeight="368.0" prefWidth="407.0">
<children>
<Button fx:id="testB" layoutX="250.0" layoutY="320.0" mnemonicParsing="false" onAction="#handleSceneShownEventTest" text="Test" />
<Label layoutX="99.0" layoutY="29.0" text="Set Running Low Thresholds:" />
<Label layoutX="78.0" layoutY="75.0" text="Concentrates:" />
<TextField fx:id="concentrateThreshold" layoutX="173.0" layoutY="71.0" prefHeight="29.0" prefWidth="100.0" />
<Label layoutX="146.0" layoutY="107.0" text="VG:" />
<TextField fx:id="vgThreshold" layoutX="173.0" layoutY="103.0" prefHeight="29.0" prefWidth="100.0" />
<Label layoutX="146.0" layoutY="139.0" text="PG:" />
<TextField fx:id="pgThreshold" layoutX="173.0" layoutY="135.0" prefHeight="29.0" prefWidth="100.0" />
<Label layoutX="110.0" layoutY="172.0" text="Nicotine:" />
<TextField fx:id="nicotineThreshold" layoutX="173.0" layoutY="168.0" prefHeight="29.0" prefWidth="100.0" />
<Label layoutX="51.0" layoutY="206.0" text="Isopropyl Alcohol:" />
<TextField fx:id="cleanerThreshold" layoutX="173.0" layoutY="202.0" prefHeight="29.0" prefWidth="100.0" />
<Button fx:id="mainMenu" layoutX="490.0" layoutY="319.0" mnemonicParsing="false" onAction="#mainMenuButton" text="Main Menu" />
<Button fx:id="submit" layoutX="344.0" layoutY="266.0" mnemonicParsing="false" onAction="#submitButton" text="Submit" visible="false" />
<CheckBox fx:id="confirm" layoutX="174.0" layoutY="271.0" mnemonicParsing="false" onAction="#confirmChanges" text="Confirm Change(s)" />
<Label layoutX="281.0" layoutY="75.0" text="ml" />
<Label layoutX="281.0" layoutY="107.0" text="ml" />
<Label layoutX="281.0" layoutY="139.0" text="ml" />
<Label layoutX="281.0" layoutY="172.0" text="ml" />
<Label layoutX="281.0" layoutY="206.0" text="ml" />
<Label layoutX="388.0" layoutY="29.0" text="Current Thresholds:" />
<Label layoutX="517.0" layoutY="74.0" text="ml" />
<Label layoutX="517.0" layoutY="106.0" text="ml" />
<Label layoutX="517.0" layoutY="138.0" text="ml" />
<Label layoutX="517.0" layoutY="171.0" text="ml" />
<Label layoutX="517.0" layoutY="205.0" text="ml" />
<Label fx:id="currentConcentrateThresholdLabel" layoutX="444.0" layoutY="74.0" text="??" />
<Label fx:id="currentVGThresholdLabel" layoutX="444.0" layoutY="106.0" text="??" />
<Label fx:id="currentPGThresholdLabel" layoutX="444.0" layoutY="138.0" text="??" />
<Label fx:id="currentNicotineThresholdLabel" layoutX="444.0" layoutY="171.0" text="??" />
<Label fx:id="currentCleanerThresholdLabel" layoutX="444.0" layoutY="205.0" text="??" />
</children>
</AnchorPane>
</children>
</VBox>
The NullPointerException gets thrown at OptionsController.currentConcentrateThresholdLabel.setText(concentrateCurrentThreshold);
at my showCurrentThresholds()
method in OptionsDB.java.
I assume that I'm missing something silly.