-1

I'm trying to get the data for the username that is used to login into my app so I can insert it into DB when an appointment is created so I can use that username to show all the apointments made by that user that logged into the app.

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

This is where the username is used :

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();
 }
 }

}
}
     ***THis is where I save the username so I can use it to add it into the     db***  

Here I'm trying to add an apointment, and along with the data that I'm getting via FXML I'm trying to add the username into the table aswell

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) {

} 

*Here's 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 it seems it's still not loading the username for some reason.

So basically either the way that I'm getting the username to insert it into the DB is wrong, or the actually insertion is wrong.

MGAMG
  • 51
  • 6
  • `String numeutilizator=Numeutilizator.getText();` – RisingSun Jun 08 '18 at 18:52
  • Thank you for ur answer! That seems to make the Sql Violation go away, but now it gives me `Caused by: java.lang.NullPointerException at LicentaApp.LogareController.getText(LogareController.java:129) at LicentaApp.AddProgramareController.AddProgramare(AddProgramareController.java:99) ... 58 more` – MGAMG Jun 08 '18 at 19:00
  • At line 129(LogareController): `String numeutilizator=Numeutilzator.getText();` At line 99(AddProgramareController): `String numeutilizator=Numeutilizator.getText();` – MGAMG Jun 08 '18 at 19:01
  • Do those classes exist? If so, do they have `getText()` functions? If so, what are they returning? The errors are telling you exactly which functions are causing the error and what is causing them. It says `NullPointerException` which means either those methods don't exist or they are returning something they are not suppose to, – RisingSun Jun 08 '18 at 19:08
  • The classes are posted in the question above, in LogareController i get the input from the username that is inserted by the user to login, via Textfield then i made a method that you corrected for me named get text, then in AddProgramareController ive called Numeutilizator by `@FXML LogareController Numeutilizator=new LogareController();`, the method created in LogareController is called getText so i can use it in when i do ` String numeutilizator=Numeutilizator.getText();` in AddProgramareController – MGAMG Jun 08 '18 at 19:12
  • I am not really the best a java, obviously, I might confuse you more then anything, I'm sorry.. – MGAMG Jun 08 '18 at 19:13
  • Possible duplicate of [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – SedJ601 Jun 08 '18 at 19:35

1 Answers1

0

The error appears to be in your getText method in the class LogareController.

You have the following:

public String getText() {
    TextField Numeutilizator = new TextField();
    Numeutilizator.getText();
    return null;
}

You are always returning null instead of the text in your textfield. You are also recreating the Numeutilizator variable locally within the method which will always be empty. You should be using the one you define at the beginning of your class instead. I believe changing the getText method to the following should fix your errors:

public String getText() {
    return this.Numeutilizator.getText();
}

Hope that helps.

NCoop
  • 341
  • 3
  • 15
  • Probably not (completely). The controller instance used doesn't seem to be used with a fxml... – fabian Jun 08 '18 at 19:16
  • It's the same, same lines returning as being the problem, thank you for ur answer though.. – MGAMG Jun 08 '18 at 19:16
  • @fabian yeah you are right, was just rethinking about it and it looks like the controller isn't instantiated correctly. – NCoop Jun 08 '18 at 19:18
  • Fabian, NCoop `<... xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LicentaApp.LogareController">` its Linked with Logare.FXML – MGAMG Jun 08 '18 at 19:18
  • @MGAMG wrong instance. See *Passing Parameters Directly From the Caller to the Controller* here: https://stackoverflow.com/a/14190310/2991525 – fabian Jun 08 '18 at 19:20
  • I've seen it, can't manage to reproduce it, is it the only way? – MGAMG Jun 08 '18 at 19:21
  • @MGAMG If you specify the controller via `fx:controller` attribute there is no other way than using `FXMLLoader.getController` after loading the fxml with the instance of the loader. (Other than using static fields to store the information, which is not a good idea...) – fabian Jun 08 '18 at 19:27
  • Ok, I'll try harder, I'll get back here if I can make any meaningful progress, meantime I'm still opened to suggestions, thank you @fabian ! – MGAMG Jun 08 '18 at 19:30
  • @fabian I think i've managed to make the passing of the parameters, or atleast tried, but it's still not actually doing the job as expected, can you take a look, ive upldated with what I've done – MGAMG Jun 09 '18 at 07:01