0

I am using the JavaFX as my GUI and JDBC as my API for database. The method saveCard() is supposed to parse text from the JavaFX TextArea and TextField which then saves the data through the database.insertQuestionAndAnswer() method. It is not and only returning an empty string.

Java code:

package workspace;

import java.io.IOException;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class CardMakerController {

    @FXML

    private CardDatabase database = new CardDatabase();
    private TextField questionContainer = new TextField();
    private TextArea answerContainer = new TextArea();

    public void changeViewToDefaultScreen(ActionEvent e) throws IOException {
        App.setRootForScene("welcomeScreen");
    }

    public void saveCard() {
        String cardQuestion = questionContainer.getText();
        String cardAnswer = answerContainer.getText();
        System.out.println("cardQuestion" + cardAnswer);
        database.insertQuestionAndAnswer(cardQuestion, cardAnswer);
    }
}

FXML file:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?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 prefHeight="373.0" prefWidth="579.0" style="-fx-background-color: khaki; -fx-border-color: orange;" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="workspace.CardMakerController">
   <columnConstraints>
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
   </rowConstraints>
   <children>
      <Label text="CREATE FLASH CARD" textAlignment="CENTER" wrapText="true" GridPane.columnIndex="2">
         <font>
            <Font name="Cascadia Code Regular" size="12.0" />
         </font>
      </Label>
      <Label text="QUESTION:" GridPane.columnIndex="1" GridPane.rowIndex="1" GridPane.valignment="TOP">
         <font>
            <Font name="Cascadia Code Regular" size="12.0" />
         </font>
      </Label>
      <Label text="ANSWER/S:" GridPane.columnIndex="1" GridPane.rowIndex="2" GridPane.valignment="BOTTOM">
         <font>
            <Font name="Cascadia Code Regular" size="12.0" />
         </font>
         <GridPane.margin>
            <Insets bottom="12.0" />
         </GridPane.margin>
      </Label>
      <TextField fx:id="questionContainer" prefHeight="25.0" prefWidth="125.0" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="1" GridPane.valignment="BOTTOM" />
      <TextArea fx:id="answerContainer" prefHeight="70.0" prefWidth="348.0" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="3" GridPane.rowSpan="2" />
      <DialogPane style="-fx-background-color: KHAKI;" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="5" />
      <Button mnemonicParsing="false" onAction="#saveCard" style="-fx-background-color: orange;" text="Save Card" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER">
         <font>
            <Font name="Cascadia Code Regular" size="12.0" />
         </font>
      </Button>
      <Button mnemonicParsing="false" onAction="#changeViewToDefaultScreen" style="-fx-background-color: orange;" text="Change View" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="5" GridPane.valignment="CENTER">
         <font>
            <Font name="Cascadia Code Regular" size="12.0" />
         </font>
      </Button>
   </children>
</GridPane>

I tried to check if the String parsed is null through an if loop to test it, but is not. When I check the database there is no word that is saved. It is empty pleas help

  • 3
    *Never* call `new` on `@FXML` fields. – jewelsea Feb 13 '23 at 02:55
  • 2
    You probably manually instantiated those UI objects because you were getting an NPE at some point and learned that meant you needed to initialize the fields. However, an object defined in the FXML file will be injected into the controller instance, so you do not need to instantiate them yourself. The problem is that both `questionContainer` and `answerContainer` are missing `@FXML` annotations. Both fields need that annotation, separately. That one `@FXML` annotation you have is only being applied to the `database` field—which is likely an error in and of itself, by the way. – Slaw Feb 13 '23 at 03:03

0 Answers0