0

I'm coding an Email-client with j=JavaFX. Now I'm trying to write down the reply button and I get the java.lang.reflect.InvocationTargetException, when I try to find the Sender of the Email to reply. Here's the Replaycontroller code:

package project;

import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javafx.fxml.FXML;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import static project.FXMLNewMailController.mittenti;

public class FXMLReplayController implements Initializable {
    @FXML
    private Button Btn_send;
    @FXML
    private TextField Txt_object;
    @FXML
    private TextField Txt_receiver;
    @FXML
    private TextArea Area_text;
    @FXML
    private AnchorPane AP_replace;

    private static String author, send;
    private static Mail mail;
    static String mittenti[] = {"marti.anzo@gmail.it", "andrea.pizza@hotmail.com", "mattia.berni@edu.unito.it"};

    public void setAuthor(String author) {
        this.author = author;
    }
    public String getAuthor() {
        return author;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //Txt_receiver.setText(getMessaggio().getSender(); //error

    }

    public void setMessaggio(Mail mail){
        this.mail = mail;
    }
    public Mail getMessaggio(){
        return mail;
    }
}

The Txt_receiver.setText(getMessaggio().getSender(); leave me the error, if I comment this section it works, but I need to get the Sender to Replay the email successfully. The getMessage() method is in this class, the getSender() one is declared in the Mail class.

The button handler code:

private void handleBtnReplay(ActionEvent event) throws IOException{
    System.out.println("Inoltra");
    try{
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLReplay.fxml"));
        Parent rootRW = (AnchorPane) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setTitle("Replay");

    FXMLReplayController control = fxmlLoader.<FXMLReplayController>getController();
    control.setMessaggio(getMail());
    System.out.println("rispondo a: " + control.getMessaggio());
    System.out.println("destinatario: " + getMail().getSender());

    stage.setScene(new Scene(rootRW));
    stage.show();
}catch (Exception e) {
    System.out.println("errore inoltra: "+ e);
}

}

I never found this error before and searching online I can't understand what's the point with this 2 get methods.

Reverendo
  • 1
  • 1
  • Where is the code that actually handles the button click? – SedJ601 Aug 30 '18 at 14:31
  • Ideally you should also post the code of the `Mail.getSender()` method because that's where the exception happens. – HugoTeixeira Aug 30 '18 at 14:33
  • Also please post the stacktrace – akortex Aug 30 '18 at 14:46
  • 1
    I suspect a `NullPointerException` to be the cause of the issue. (You should be able to verify this by looking at the stacktrace (the causes should contain the exception that is the real source of the issue; Every exception resulting from a call of an event handler is wrapped in a `InvokationTargetException`)) If this is indeed the case, please refer to this question https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it . (Should you still not be able to identify the issue, describe, why you expect the `mail` field to eb set and where this happens.) – fabian Aug 30 '18 at 15:16
  • @Sedrick I edited my post with the handler – Reverendo Aug 30 '18 at 15:30
  • @HugoTeixeira The problem is only with the method getMessaggio(), we use getSender() in another method and it works fine. – Reverendo Aug 30 '18 at 15:35
  • @fabian Txt_receiver.setText(getMessaggio().getSender(); if I comment this section the method getMessaggio() print the correct email. If I uncomment Txt_receiver.setText(getMessaggio().getSender() I get the java.lang.reflect.InvocationTargetException error. – Reverendo Aug 30 '18 at 15:49

0 Answers0