mysql—将参数从一个控制器传递到另一个控制器会产生java.lang.nullpointerexception

lo8azlld  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(444)

我正在尝试获取某人用于登录的用户名,然后在某人发出提示时将其插入数据库
这是“programaritest”表

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

这是accounts表,其中用户名保存为unique

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

用户名的输入是通过一个textfield从fxml获得的,在getusername方法中,我在下面指出了这一点

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

}
}

下面是一个将apointment添加到db的类,我在下面的代码中指出了用户名的函数是什么(我添加了所有代码,以便您更好地理解发生了什么)

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

}
}

尝试添加新约会时收到的错误消息是:
由以下原因引起:java.lang.nullpointerexception at licenstapp.addprogramarecontroller.addprogramare(addprogramarecontroller)。java:103)
在第103行:string numeutilizator=numeutilizator.gettext();
所以来自numeutilizator的数据不是输入输出的数据,这意味着我无法将参数从一个控制器传递到另一个控制器。
p、 我跟踪了这个传递参数javafx-fxml,搞不懂。

xt0899hw

xt0899hw1#

发生此异常的原因有多种:
控制器示例未与fxml一起使用
与控制器示例一起使用的fxml不包含具有的元素 fx:id="Numeutilizator" 因此,我将发布一个简单的示例,从使用fxml设计的场景中获取单个字符串:

应用程序类

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

对话框.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);
    }

}

在显示fxml的内容之前,您还可以传递一个包含逻辑的对象来处理某些事件,而不是检索信息。

相关问题