0

This is my code snippet returning NullPointerException on garmentFactory

GarmentFactoryApplicationLauncher loads the /config/applicationContext.xml

public class GarmentFactoryApplicationLauncher extends Application{

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/fxml/clientInterface.fxml"));
    stage.setScene(new Scene(root));
    stage.setResizable(false);
    stage.initStyle(StageStyle.UTILITY);
    stage.setTitle("GarmentFactory Factory");
    stage.show();
}

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("/config/applicationContext.xml");
    launch(args);
  }
}

this is my applicationContext.xml file under resources/config folder

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.design"/>

</beans>

error occurs in this file, saying NullPointerException on garmentFactory

@Component
public class ClientInterfaceController implements Initializable {

    @FXML
    private ChoiceBox garmentSelectionChoiceBox;

    @FXML
    private TextField displaySelectionTextField;

    @Autowired
    private GarmentFactory garmentFactory;

    public void initialize(URL location, ResourceBundle resources) {

    }

    /**
     * call the garment factory to produce garment of specific type
     */
    @FXML
    public void OnClickGetBtn(){
        String garmentCode  = (String)garmentSelectionChoiceBox.getSelectionModel().getSelectedItem();
        Garment garment = garmentFactory.getGarment(garmentCode);
        displaySelectionTextField.setText(garment.toString());

    }
}

the GarmentFactory is annotated with @Component and is basic java class with some logic

What am I missing/doing wrong

Thanks

Dota2
  • 456
  • 4
  • 16
  • how do you invoke public class ClientInterfaceController.OnClickGetBtn. Source of that class would be usefull – Maciej Kowalski Jan 22 '17 at 14:59
  • @MaciejKowalski in correspoding fxml file 'Get' Button has property onAction="#onClickGetBtn" – Dota2 Jan 22 '17 at 15:37
  • 1
    The controller instance is being created by the `FXMLLoader`, not by the Spring application context. Obviously, the `FXMLLoader` knows nothing about injecting spring beans. – James_D Jan 22 '17 at 16:42
  • @James_D can you be more descriptive – Dota2 Jan 22 '17 at 16:47
  • Not really: that's all there is to it. The linked question is essentially identical and provides the solution. – James_D Jan 22 '17 at 16:48

0 Answers0