0

I wish to access value of selected item from the Combobox of one controller class in other controller class.

I am having three values "Easy", "Medium" and "Difficult" in the combobox observablelist of one controller class. Lets say, I select "Easy" option and proceed to other class, then I wish to access its value which is 'Easy' in this case.

I have declared my Combobox in the first class as follows :

public ComboBox combobox = new ComboBox();

In other class, I am writing following code to access its value on a button click :

HomefxmlController hfc = new HomefxmlController();
System.out.println(hfc.combobox.getSelectionModel().getSelectedItem());

It returned NULL

I further tried to access it using a method with return type "String", but got a NullPointerException instead.

Can anyone tell me how to resolve it and access its item value from a completely different class ?

FYI : This has no relation between changing scenes and accessing other FXML controller class, I am having problem on accessing object values.

Vishal A
  • 144
  • 3
  • 17
  • Please read the FYI note. There is no problem in accessing other scene, the problem lies only while accessing combobox values. Kindly read my question properly and try to help if you can, else I can wait. – Vishal A Mar 23 '16 at 12:21
  • So you want the choice to be assigned before the user has time to select the value? – fabian Mar 23 '16 at 12:24
  • No. I wish to access the choice, as in what was the choice the user selected.When I do it at the moment, it returns a big NULL ! – Vishal A Mar 23 '16 at 12:27
  • Why would you expect anything in that controller to be initialized? Controller fields are typically initialized when the `FXMLLoader` loads an FXML file. Here you just create the controller instance yourself. Why would the combo box even be initialized, let alone have a value selected? – James_D Mar 23 '16 at 13:02

0 Answers0