简体   繁体   中英

NullPointerException while accessing fxml element from controller?

My current project is structured like this
MainStage
|_ Main Controller
|_ MainScene.fxml

public class MainController implements Initializable {
@FXML
public void switchToEmail() throws Exception{
   EmailController ec = new EmailController();
   ec.display();
}

I loaded MainController in MainScene.fxml using fx:controller I have another stage created
EmailStage
|_Email Controller |_Email Scene

public class EmailController implements Initializable{
HashMap emails;
@FXML
public static TextArea emailSceneTextArea;
@FXML
public void display(){
emails = new HashMap<Integer, String>();

String responseEmails = null;
System.out.println(emailSceneTextArea.getText());
try {
responseEmails = this.getEmails().trim();
} catch (Exception ex) {
System.out.println("responseEmail Exception in EmailController.java");
}

emails = ParseXml.parse(responseEmails, "emails");
Set s = emails.keySet();
Iterator it = s.iterator();
while (it.hasNext()) {
Integer i = (Integer)it.next();
String text = emailSceneTextArea.getText(); /*NullPointerException Line*/
String email = (String) emails.get(i);
emailSceneTextArea.setText(text+email);

}


}

public String getEmails() throws Exception{
RequestHandler rq = new RequestHandler();
return rq.prepare("fetchEmails", "nothing");
}

And I loaded EmailController in emailScene.fxml using fx:controller
When I switch stage to emailStage in EmailController I get 'NullPointerException' at line mentioned. Where am I doing wrong?

PS = After wasting couple of hours when I did call display method from a button action of emailScene.fxml. It worked. Confused why?

Correct me if I am wrong, but...what data is stored in emailSceneTextArea.

It appears like you are trying to get text that doesn't exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM