JavaFX变量初始化失败[重复]

jaql4c8m  于 2022-12-10  发布在  Java
关注(0)|答案(1)|浏览(149)

此问题在此处已有答案

What is a NullPointerException, and how do I fix it?(12个答案)
昨天关门了。
我希望将用户输入存储在Scene 1中的privateKey和publicKey变量中的一个简单文本字段中,格式为byteArray,如下所示:我在代码中用--〉标记了相应的行

//Scene 1
private Stage stage;
private Scene scene;
private Parent root;
private byte[] publicKey;  <------
private byte[] privateKey; <------

@FXML
private Button button;

@FXML
private TextField pubKeyField;

@FXML
private TextField privKeyField;

@FXML
void loginClicked(ActionEvent event) {
    System.out.println("Eingeloggt");
}

public void switchToScene2(ActionEvent event) {
    try {
        this.publicKey = SHA3Helper.hexToDigest(pubKeyField.getText()); <----
        this.privateKey = SHA3Helper.hexToDigest(privKeyField.getText()); <----
    
        Parent root = FXMLLoader.load(getClass().getResource("../main/Scene.fxml"));
        stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
        scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

SHA 3 Heloer将字符串转换为byteArray,这是正确的。然后我想在第二个场景中使用现在初始化的变量:

@FXML
void submitPressed(ActionEvent event) {
    System.out.println("Submit Pressed");
    //publicKey = SHA3Helper.hexToDigest(senderField.getText());
    byte[] receiver = SHA3Helper.hexToDigest(receiverField.getText());
    double amount = Double.parseDouble(amountField.getText());
    double tfbp = Double.parseDouble(tfbpField.getText());
    double tfl = Double.parseDouble(tflField.getText());
    //privateKey = SHA3Helper.hexToDigest(keyField.getText());
    
    ---> Transaction transaction = new Transaction(this.publicKey, receiver, amount, 0, tfbp, 
    tfl);
    System.out.println(transaction.getAmount());
    try {
        byte[] signature = SignatureHelper.GenerateSignature(transaction.asJSONString(), 
      ---> this.privateKey);
        transaction.setSignature(signature);
        System.out.println("Signatur: " + SHA3Helper.digestToHex(transaction.getSignature()));
        boolean result = SignatureHelper.ValidateSignature(transaction.asJSONString(), 
    --->  this.publicKey, signature);
        System.out.println("Verification result: " + result);
    } catch (Exception e) {     
        e.printStackTrace();
    }
    if(VerificationHelper.verifyTransaction(transaction)) {
        DependencyManager.getPendingTransactions().addPendingTransaction(transaction);
        try {
            DependencyManager.getBlockchainNetwork().sendTransaction(transaction);
            System.out.println("TransactionSent");
        } catch (Exception e) {
            
            e.printStackTrace();
        }
    }

}

但是现在我得到“java.lang.NullPointerException:无法读取数组长度,因为“”为空”我知道空指针异常是什么,但我无法解释为什么变量为空??正如您所看到的,我在scene 1中使用UserInput将它们初始化到TextField中。当尝试将所有内容放入“submitPressed”方法时,它工作得很好。但如果我之前尝试初始化两个变量,就不行了。

dfuffjeb

dfuffjeb1#

解决方案是为LoginScene创建第二个Controller类,使Variables的私钥和公钥为静态,并通过MainScene的ControllerClass中的getter访问它们。

public class ControllerLoginScene {
//Scene 1
private Stage stage;
private Scene scene;
private Parent root;
private static byte[] publicKey;
private static byte[] privateKey;

@FXML
private Button button;

@FXML
private TextField pubKeyField;

@FXML
private TextField privKeyField;

@FXML
void loginClicked(ActionEvent event) {
    System.out.println("Eingeloggt");
}

public void switchToScene2(ActionEvent event) {
    try {
        this.publicKey = SHA3Helper.hexToDigest(pubKeyField.getText());
        this.privateKey = SHA3Helper.hexToDigest(privKeyField.getText());
    
        Parent root = FXMLLoader.load(getClass().getResource("../main/Scene.fxml"));
        stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
        scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static byte[] getPublicKey() {
    return publicKey;
}

public static byte[] getPrivateKey() {
    return privateKey;
}

}
主控制器访问:

@FXML
void submitPressed(ActionEvent event) {
    System.out.println("Submit Pressed");
    byte [] publicKey = ControllerLoginScene.getPublicKey();
    byte[] receiver = SHA3Helper.hexToDigest(receiverField.getText());
    double amount = Double.parseDouble(amountField.getText());
    double tfbp = Double.parseDouble(tfbpField.getText());
    double tfl = Double.parseDouble(tflField.getText());
    byte [] privateKey = ControllerLoginScene.getPrivateKey();

相关问题