0

I'm doing a calendar, where if I press to one Label, a second window is showed up to make the new appoinment.

I have for all days, one method where I receive the event. When I try to get the information from this method(the public String aux)

public void labClick(MouseEvent event){

    try {

        Label aab=(Label) event.getSource();
        aux=aab.getText();
        Parent root;
        root = FXMLLoader.load(getClass().getResource("/application/cita.fxml"));
        Stage stage = new Stage();
        stage.setTitle("New Appoinment");
        stage.setScene(new Scene(root, 500, 500));
        stage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }   
}

and try to use in this one:

public void insertCita(Event event){
    String dia;
    String inic;
    String fin;
    String stuf;
    Appoinment app;
    inic=startTime.getText();
    fin=finishTime.getText();
    stuf=stuff.getText();
    System.out.println(aux);
}

I get a null value when I print the aux variable.

Any ideas?

edit:

This is the cita .fxml file

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="185.0" prefWidth="341.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.Main.">
  <!-- TODO Add Nodes -->
  <children>
    <VBox layoutX="0.0" layoutY="0.0" prefHeight="185.0" prefWidth="341.0">
      <children>
        <TextField fx:id="startTime" prefHeight="35.0" prefWidth="341.0" text="Type starting time" />
        <TextField fx:id="finishTime" prefHeight="35.0" prefWidth="341.0" text="Type finishing time" />
        <TextField fx:id="stuff" prefHeight="35.0" prefWidth="341.0" text="Type stuff to do" />
        <Button fx:id="goBut" contentDisplay="LEFT" mnemonicParsing="false" onMouseClicked="#insertCita" prefHeight="52.0" prefWidth="90.0" text="Go" />
      </children>
    </VBox>
  </children>
</AnchorPane>

And this is the main .fxml file:

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>

<GridPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.Main">
  <children>
    <GridPane>
      <children>
        <GridPane>
          <children>
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Monday" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Tuesday" wrapText="true" GridPane.columnIndex="2" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Wednesday" wrapText="true" GridPane.columnIndex="3" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Thursday" wrapText="true" GridPane.columnIndex="4" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Friday" wrapText="true" GridPane.columnIndex="5" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Saturday" wrapText="true" GridPane.columnIndex="6" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Sunday" wrapText="true" GridPane.columnIndex="7" GridPane.rowIndex="0" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Monday" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Tuesday" wrapText="true" GridPane.columnIndex="2" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Wednesday" wrapText="true" GridPane.columnIndex="3" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Thursday" wrapText="true" GridPane.columnIndex="4" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Friday" wrapText="true" GridPane.columnIndex="5" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Saturday" wrapText="true" GridPane.columnIndex="6" GridPane.rowIndex="9" />
            <TextArea editable="false" mouseTransparent="false" prefWidth="200.0" text="Sunday" wrapText="true" GridPane.columnIndex="7" GridPane.rowIndex="9" />
            <Button id="prev" fx:id="prev2" mnemonicParsing="false" onAction="#ClickMinus" prefHeight="30.0" prefWidth="70.0" text="prev" GridPane.columnIndex="8" GridPane.rowIndex="0" />
            <Button fx:id="next" mnemonicParsing="false" onAction="#ClickPlus" prefHeight="29.999900000002526" prefWidth="70.00009999999747" text="next" GridPane.columnIndex="0" GridPane.rowIndex="9" />
            <Button fx:id="next2" mnemonicParsing="false" onAction="#ClickPlus" prefHeight="30.0" prefWidth="70.0" text="next" GridPane.columnIndex="8" GridPane.rowIndex="9" />
            <Button fx:id="prev" mnemonicParsing="false" onAction="#ClickMinus" prefHeight="30.0" prefWidth="70.0" text="prev" GridPane.columnIndex="0" GridPane.rowIndex="0" />
            <TextArea fx:id="week1" prefWidth="200.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="2" />
            <TextArea fx:id="week2" prefWidth="200.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="4" />
            <TextArea fx:id="week4" prefWidth="200.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="8" />
            <Label fx:id="lab11" onMouseClicked="#labClick" prefHeight="40.0" prefWidth="150.0" text="" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <Label fx:id="lab12" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="1" />
            <Label fx:id="lab13" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="1" />
            <Label fx:id="lab14" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="1" />
            <Label fx:id="lab15" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="1" />
            <Label fx:id="lab21" minHeight="13.0" onMouseClicked="#labClick" prefHeight="40.0" prefWidth="149.9998779296875" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <Label fx:id="lab22" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="3" />
            <Label fx:id="lab23" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="3" />
            <Label fx:id="lab32" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="5" />
            <Label fx:id="lab31" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" />
            <Label fx:id="lab33" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="5" />
            <Label fx:id="lab34" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="5" />
            <Label fx:id="lab24" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="3" />
            <Label fx:id="lab25" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="3" />
            <Label fx:id="lab35" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="5" />
            <Label fx:id="lab41" onMouseClicked="#labClick" prefHeight="40.000099999997474" prefWidth="150.0" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="7" />
            <Label fx:id="lab42" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="7" />
            <Label fx:id="lab16" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="1" />
            <Label fx:id="lab17" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="1" />
            <Label fx:id="lab26" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="3" />
            <Label fx:id="lab43" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="3" GridPane.rowIndex="7" />
            <Label fx:id="lab44" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="4" GridPane.rowIndex="7" />
            <Label fx:id="lab45" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="5" GridPane.rowIndex="7" />
            <Label fx:id="lab36" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="5" />
            <Label fx:id="lab46" onMouseClicked="#labClick" prefHeight="39.9998779296875" prefWidth="150.0" text="Label" GridPane.columnIndex="6" GridPane.rowIndex="7" />
            <Label fx:id="lab47" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="7" />
            <Label fx:id="lab37" onMouseClicked="#labClick" prefHeight="40.000099999997474" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="5" />
            <Label fx:id="lab27" onMouseClicked="#labClick" prefHeight="44.0" prefWidth="139.0" text="Label" GridPane.columnIndex="7" GridPane.rowIndex="3" />
            <TextArea fx:id="week3" prefHeight="100.00009999999747" prefWidth="70.0" text="Week x Year x" wrapText="true" GridPane.columnIndex="0" GridPane.rowIndex="6" />
            <Button fx:id="start" mnemonicParsing="false" onAction="#ClickStart" prefHeight="30.0" prefWidth="70.0" text="Start" GridPane.columnIndex="0" GridPane.rowIndex="1" />
            <VBox fx:id="vb11" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <VBox fx:id="vb12" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb13" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb21" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb22" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="2" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb23" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb25" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="5" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb31" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb32" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="2" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb14" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="4" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb15" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="5" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb35" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="5" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb33" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb41" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb42" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="2" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb43" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb44" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="4" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb45" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="5" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb24" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="4" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb26" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="6" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb36" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="6" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb16" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="6" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb17" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="7" GridPane.rowIndex="2" />
            <VBox id="vb12" fx:id="vb27" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="7" GridPane.rowIndex="4" />
            <VBox id="vb12" fx:id="vb37" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="7" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb34" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="4" GridPane.rowIndex="6" />
            <VBox id="vb12" fx:id="vb46" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="6" GridPane.rowIndex="8" />
            <VBox id="vb12" fx:id="vb47" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="7" GridPane.rowIndex="8" />
          </children>
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="70.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="150.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="70.0" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="100.0" minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="100.0" minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="100.0" minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="100.0" minHeight="10.0" prefHeight="100.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="40.0" minHeight="10.0" prefHeight="40.0" vgrow="SOMETIMES" />
          </rowConstraints>
        </GridPane>
      </children>
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
  </children>
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
  </rowConstraints>
</GridPane>
  • Where and how is `aux` defined? – Jérôme Mar 27 '17 at 23:05
  • public String aux; just after making the main class. – Javier Bertoméu Muñoz Mar 27 '17 at 23:06
  • Are these methods in the controllers? If I understand correctly, `insertCita(...)` is a controller method for something in `cita.fxml` and `labClick` is a controller method for something in another FXML file. If that's correct, how are you passing `aux` from one controller to the other? – James_D Mar 27 '17 at 23:10
  • 1
    Can you provide some more code so we can see your class structure and the calls of the `insertCriteria` – Jérôme Mar 27 '17 at 23:12

1 Answers1

0

The aux value is set in the labClick method, which is called in the controller for the main FXML file; however you are then trying to access it in the controller for cita.fxml, where it was never initialized.

You are using the same class for the controllers from both FXML files, which will always lead to this kind of confusion, as it's extremely difficult to track which variables are initialized in which controllers. I strongly recommend you use a different class for the controllers for each FXML file.

Either way, you need to pass the value from one controller to the other when you load cita.fxml. The ways to do this are outlined in Passing Parameters JavaFX FXML, but briefly you would define a CitaController class for use with cita.fxml, give it a setAux() method:

public class CitaController {

    private String aux ;

    public void setAux(String aux) {
        this.aux = aux ;
    }

    @FXML
    private void insertCita(Event event){ 
        // existing code...
    }

    // ...
}

and then load the FXML with

Label aab=(Label) event.getSource();
aux=aab.getText();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/cita.fxml"));
Parent root = loader.load();

CitaController citaController = loader.getController();
citaController.setAux(aux);

// ...
Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322