0

I'm trying to get the username that somebody uses to Login, then insert it into a db when somebody makes an apointment

This is the "Programaritest" table

ID_programare | Nume | Prenume | Data | Ora | Departament | Doctor | Username | Nr_telefon

This is the accounts table where the username is saved as Unique

ID_account | Username | Password | Email | Nume |  Prenume | Admin

The input of the username is gotten from fxml via a textfield, in GetUsername method, I've pointed it out below

package LicentaApp;



public class LogareController implements Initializable {
 public LoginVerifier loginVerifier = new LoginVerifier();





   @FXML
   private TextField Numeutilzator; Numeutilziator is the username that I am talking about

   @FXML
   private PasswordField Parola;

   @FXML
   private Label Stare;









 @Override
 public void initialize(URL location, ResourceBundle resources) {


  if (loginVerifier.Conexiune()) {
      Stare.setText("");
  } else {

      Stare.setText("Conexiune nereusita!");

  }

 }

 public void Autentificare (ActionEvent event) {
 try {
         if(loginVerifier.testaredate(Numeutilzator.getText(),     Parola.getText())) {
         Stare.setText("Autentificare reusita !");
         ((Node)event.getSource()).getScene().getWindow().hide();
            Stage StagePrincipala= new Stage();
            FXMLLoader incarcator= new FXMLLoader();
            Pane parinte = incarcator.load(getClass().getResource("/LicentaApp/Meniu.fxml").openStream());

            Scene scene = new Scene(parinte);
                scene.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            StagePrincipala.setScene(scene);
            StagePrincipala.show();

          }
     else { 
         Stare.setText("Nume de utilizator sau parola incorect");

     }




} catch (SQLException e) {





     e.printStackTrace();
} catch (IOException e) {

    e.printStackTrace();
    }
 }
 @FXML
    public void Inregistrare(ActionEvent event) {
        try {
         ((Node)event.getSource()).getScene().getWindow().hide();
            Stage PS= new Stage();
            FXMLLoader incarcator= new FXMLLoader();
            Pane parinte = incarcator.load(getClass().getResource("/LicentaApp/InregistrareUser.fxml").openStream());
            Scene scena = new Scene(parinte);
            scena.getStylesheets().add(getClass().getResource("Style1212.css").toExternalForm());
            PS.setScene(scena);
            PS.show();
    } catch (Exception e) {

    }

*Here I m calling the function that I made in AddProgramareController and 
passing it the username located in Numeutilzator*

@FXML 
 public void GetUsername() {
     try {
     FXMLLoader loader=new     FXMLLoader(getClass().getResource("/LicentaApp/AddProgramare.fxml"));
     Parent root = (Parent) loader.load();

     AddProgramareController AddPr=loader.getController();
     AddPr.MyUsername(Numeutilzator.getText());


 } catch (IOException e) {
     e.printStackTrace();
 }
 }

}
}

Here is the class that adds the apointment to the db, I've pointed out whre the function for the username is, in the code below (I've added all the code so you can understand better what's going on)

package LicentaApp;



public class AddProgramareController implements Initializable {
    ObservableList Timestamp=FXCollections.observableArrayList();




@FXML
private TextField Nume;

@FXML
private TextField Prenume;

@FXML
private TextField Ora;

@FXML
private DatePicker Data;

@FXML
private TextField Departament;

@FXML
private TextField Doctor;

@FXML
private TextField Nr_telefon;

@FXML
private TextField Numeutilizator;













public void initialize(URL location, ResourceBundle resources) {

} 

*This is the function that I'll call in GetUsername from LogareController
 public void MyUsername(String Numeutilizator) {
    this.Numeutilizator.setText(Numeutilizator);

}

@FXML 
private void AddProgramare(ActionEvent event) throws SQLException, IOException  {


    String Interogare1= "INSERT INTO programaritest(Nume,Prenume,Data,Ora,Departament,Doctor,Nr_telefon,Numeutilizator) VALUES(?,?,?,?,?,?,?,?)";

    String nume=Nume.getText();
    String prenume=Prenume.getText();

    LocalDate data=Data.getValue(); 

    String ora=Ora.getText();
    String departament=Departament.getText();
    String doctor=Doctor.getText();
    String nr_telefon=Nr_telefon.getText();
    String numeutilizator=Numeutilizator.getText();

    try {
        ConectaredB ConectaredB=new ConectaredB();
        Connection conexiune=ConectaredB.logareDB();
        PreparedStatement MG = conexiune.prepareStatement(Interogare1);


        MG.setString(1, nume);
        MG.setString(2, prenume);
        MG.setDate(3, Date.valueOf(data)); 
        MG.setString(4, ora);
        MG.setString(5, departament);
        MG.setString(6, doctor);
        MG.setString(7, nr_telefon);
        MG.setString(8, numeutilizator);

        MG.executeUpdate();

        // ...
   } catch (SQLException exceptie1) {
       exceptie1.printStackTrace(); 
   }


}
}

The error message that I am getting when trying to add a new appointment is:

Caused by: java.lang.NullPointerException at LicentaApp.AddProgramareController.AddProgramare(AddProgramareController.java:103)

At line 103: String numeutilizator=Numeutilizator.getText();

So the data from numeutilizator is not what the input is putting out, that means i failed passing the parameter from a controller to another.

P.S I have followed this Passing Parameters JavaFX FXML, couldn't figure it out.

MGAMG
  • 51
  • 6
  • If you get this exception, you don't use the instance controller with a fxml that contains a element with a attribute `fx:id="Numeutilizator"`. – fabian Jun 09 '18 at 10:14
  • The fx:id="Numeutilizator" is found in the Logare.FXML, which is linked with LogareController, in this line, if that's what ur refering to `FXMLLoader loader=new FXMLLoader(getClass().getResource("/LicentaApp/AddProgramare.fxml"));` I'm appealing the controller which will receive the username(Numeutilizator), i got that right, yes ? – MGAMG Jun 09 '18 at 10:18

1 Answers1

0

There are multiple reasons this exception could happen:

  • The controller instance is not used with a fxml
  • the fxml the controller instance is used with does not contain a element with fx:id="Numeutilizator"

Therefore I'll post a simple example that gets a single string from a scene designed using fxml:

Application class

@Override
public void start(Stage primaryStage) {
    Button btn = new Button("Get input");
    btn.setOnAction((ActionEvent event) -> {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("dialog.fxml"));
        Parent p;
        try {
            p = loader.load();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        DialogController controller = loader.getController();

        Stage stage = new Stage(StageStyle.UTILITY);
        stage.initOwner(primaryStage);
        stage.setScene(new Scene(p, 100, 100));

        stage.showAndWait(); // display window and wait for close

        if (controller.isCanceled()) {
            System.out.println("canceled");
        } else {
            System.out.println("input submitted: " + controller.getText());
        }
    });

    StackPane root = new StackPane(btn);

    Scene scene = new Scene(root, 300, 300);

    primaryStage.setScene(scene);
    primaryStage.show();
}

dialog.fxml

<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.DialogController">
    <children>
        <TextField fx:id="input"/>
        <Button text="submit" onAction="#submit"/>
        <Button text="cancel" onAction="#cancel"/>
    </children>
</VBox>

fxml.DialogController

public class DialogController {
    @FXML
    private TextField input;

    private boolean canceled = true;

    public boolean isCanceled() {
        return canceled;
    }

    public final String getText() {
        return input.getText();
    }

    private void close(boolean canceled) {
        this.canceled = canceled;
        input.getScene().getWindow().hide();
    }

    @FXML
    private void submit() {
        close(false);
    }

    @FXML
    private void cancel() {
        close(true);
    }

}

Instead of retrieving the info you could also pass a object containing logic to handle certain events to the controller before showing the fxml's content.

fabian
  • 80,457
  • 12
  • 86
  • 114