-2
public void setBtnAction(ActionEvent event) {
    String leagueNameString = nameFld.getText();

    if ((leagueNameString == null) || (leagueNameString == "")) {
        nameFld.setPromptText("Enter value league name");
    } else {
        CoreAppFXMLController cp = new CoreAppFXMLController();
        cp.nameOfTheLeague.setText(leagueNameString);
    }
}

}

I tried with both, text and label, but none of them works. nameOfTheLeague is protected in CoreAppFXMLController class. It returns me NullPointerException. id's are ok in fxml.

default locale
  • 13,035
  • 13
  • 56
  • 62
tadija
  • 23
  • 3
  • 6
  • 2
    Not again: `leagueNameString == ""`. Check [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Also your code uses `leagueNameString`, not `nameOfTheLeague`. – Pshemo Aug 02 '14 at 14:40
  • oh dear.. I changed to (leagueNameString.trim().equals("")) but stills returns NullPointerException. nameOfTheLeague is label, and leagueNameString is String from textfield. – tadija Aug 02 '14 at 14:52
  • Post code which will actually [let us reproduce your problem](http://sscce.org/). Otherwise we can only guess what is wrong with it. Also `cp` is local variable of `setBtnAction` method. Is it possible that you wanted to initialize `cp` which would be your class field? – Pshemo Aug 02 '14 at 14:53
  • Nope, I just need cp to access nameOfTheLeague. I have; protected Label nameOfTheLeague. I could use; protected static Label nameOfTheLeague; and then CoreAppFXMLController.nameOfTheLeague.setText(nameOfTheLeague).. Anyway it doesn't work. – tadija Aug 02 '14 at 15:11
  • Ok here it is.. If you need anything else let me know.. in this class http://pastebin.com/AK5BCjKw I have my main stage and within, a menu. In that menu I have set league name option and then this one pops up http://pastebin.com/SKNrVirj . Its simple settext on click but it doesn't work.. – tadija Aug 02 '14 at 17:25

1 Answers1

0

The way you create CoreAppFXMLController, you just invoke the constructor. Nothing else will be done. That means that no nodes are created, since no fxml file is loaded and of course cp.nameOfTheLeague is never assigned.

You have to use a FXMLLoader to create Nodes and controller from a fxml file and make the assignments for the Nodes with ids.

If you don't know how to use a FXMLLoader to get the controller, it's demonstrated in this post: https://stackoverflow.com/a/24716026/2991525

Also, if you compare Strings use string1.equals(string2), not string1 == string2.

Community
  • 1
  • 1
fabian
  • 80,457
  • 12
  • 86
  • 114
  • I dont need any fxml loaded. It is already loaded at the start. I just need to change the label of main fxml file which controller is CoreAppFXMLController. I have protected Label nameOfTheLeague; and in my other controller I have a method that checks for input and sets the label. I will try with ur suggestion. – tadija Aug 02 '14 at 15:01