JavaFX Spring Boot @Autowire服务是空的

时间:2017-11-09 09:57:25

标签: java spring javafx nullpointerexception autowired

每当我尝试访问 drivingSchoolService 字段时,我都会在java.lang.NullPointerException获得AddWindowController.java:33,但在我的生活中,我无法找出错误。我已经自动装配在另一个控制器中的相同字段,它工作得很好,但由于某种原因它不会在这里工作。由于这两个类都在同一个包中,我认为这不是一个扫描问题。

非常感谢任何帮助。

AddWindowController:

@Component
public class AddWindowController {
@FXML
private TextField firstNameText;

@FXML
private TextField lastNameText;

@Autowired
private DrivingSchoolService drivingSchoolService;

@FXML
public void handleOkAction(ActionEvent e) {
    Student s = new Student();
    s.setFirstName(firstNameText.getText());
    s.setLastName(lastNameText.getText());
        drivingSchoolService.save(s);

    Stage stage = (Stage) firstNameText.getScene().getWindow();
    stage.close();
 }
}

DrivingSchoolController(即使代码几乎相同,也没有错误)

@Component
public class DrivingSchoolController {

@FXML
private TableView<Student> studentTable;

@FXML
private TableColumn<Student, String> firstNameColumn;
@FXML
private TableColumn<Student, String> lastNameColumn;

@FXML
private TableView<Payment> tasksTable;
@FXML
private TableColumn<Payment, String> taskNameColumn;
@FXML
private TableColumn<Payment, String> taskDescriptionColumn;
@FXML
private TableColumn<Payment, PaymentType> taskStatusColumn;

@Autowired
private DrivingSchoolService drivingSchoolService;

@FXML
public void initialize() {

    configureProjectsTable();
    configureTasksTable();

    for (Student student : drivingSchoolService.findAllStudents()) {
        studentTable.getItems().add(student);
    }

    studentTable.getSelectionModel().selectFirst();
}
private void configureTasksTable() {

    taskNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
    taskDescriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description"));

    taskStatusColumn.setCellValueFactory(new PropertyValueFactory<>("status"));
    taskStatusColumn.setCellFactory(ComboBoxTableCell.forTableColumn(PaymentType.values()));

    taskStatusColumn.setOnEditCommit(edit -> {
        edit.getRowValue().setPaymentType(edit.getNewValue());
        drivingSchoolService.save(edit.getRowValue());
    });
}

private void configureProjectsTable() {

    firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
    lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));

    ChangeListener<Student> projectSelectionChanged = (observable, oldValue, newValue) -> {
        tasksTable.getItems().clear();
        for (Payment task : drivingSchoolService.findAllPaymentsByStudent(newValue)) {
            tasksTable.getItems().add(task);
        }
    };

    studentTable.getSelectionModel().selectedItemProperty().addListener(projectSelectionChanged);
}

@FXML
protected void handleAddButtonAction(ActionEvent event) throws Exception{
    try{
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/fxml/add_window.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.initStyle(StageStyle.UNDECORATED);
        stage.setTitle("ABC");
        stage.setScene(new Scene(root1));
        stage.show();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

@FXML
protected void handleRemoveButtonAction(ActionEvent event) {
    drivingSchoolService.delete(studentTable.getSelectionModel().getSelectedItem());
    refreshStudentTable();
}

public void refreshStudentTable() {
    studentTable.getItems().removeAll(studentTable.getItems());
    studentTable.getItems().addAll(drivingSchoolService.findAllStudents());
}

服务:

@Service
public interface DrivingSchoolService {
List<Student> findAllStudents();
List<Payment> findAllPayments();
List<Examination> findAllExaminations();
List<Payment> findAllPaymentsByStudent(Student student);

void save(Student student);
void save(Payment payment);
void save(Examination examination);

void delete(Student student);

}

服务实施:

@Component
public class DrivingSchoolServiceImpl implements DrivingSchoolService{
@Autowired
private StudentRepository studentRepository;
@Autowired
private PaymentRepository paymentRepository;
@Autowired
private ExaminationRepository examinationRepository;

@Override
public List<Student> findAllStudents() {
    return studentRepository.findAll();
}

@Override
public List<Payment> findAllPayments() {
    return paymentRepository.findAll();
}

@Override
public List<Examination> findAllExaminations(){
    return examinationRepository.findAll();
}

@Override
public List<Payment> findAllPaymentsByStudent(Student student) {
    return paymentRepository.findAllPaymentsByStudent(student);
}

@Override
public void save(Student student) {
    studentRepository.save(student);
}

@Override
public void save(Payment payment) {
    paymentRepository.save(payment);
}

@Override
public void save(Examination examination) {
    examinationRepository.save(examination);
}

@Override
public void delete(Student student) {
    studentRepository.delete(student);
 }
}

堆栈追踪:

2017-11-09 13:32:48.538  INFO 11352 --- [JavaFX-Launcher] o.s.boot.SpringApplication               : Starting application on Cristian-PC with PID 11352 (started by Cristian in C:\Users\Cristian\IdeaProjects\drivingschool)
2017-11-09 13:32:48.542  INFO 11352 --- [JavaFX-Launcher] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default2017-11-09 13:32:48.577  INFO 11352 --- [JavaFX-Launcher] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ef462e7: startup date [Thu Nov 09 13:32:48 EET 2017]; root of context hierarchy2017-11-09 13:32:49.825  INFO 11352 --- [JavaFX-Launcher] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'2017-11-09 13:32:49.837  INFO 11352 --- [JavaFX-Launcher] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [  name: default   ...]2017-11-09 13:32:49.893  INFO 11352 --- [JavaFX-Launcher] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}2017-11-09 13:32:49.894  INFO 11352 --- [JavaFX-Launcher] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found2017-11-09 13:32:49.895  INFO 11352 --- [JavaFX-Launcher] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist2017-11-09 13:32:49.985  INFO 11352 --- [JavaFX-Launcher] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}2017-11-09 13:32:50.082  INFO 11352 --- [JavaFX-Launcher] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect2017-11-09 13:32:50.466  INFO 11352 --- [JavaFX-Launcher] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update2017-11-09 13:32:50.565  INFO 11352 --- [JavaFX-Launcher] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'2017-11-09 13:32:50.915  INFO 11352 --- [JavaFX-Launcher] o.s.boot.SpringApplication               : Started application in 2.659 seconds (JVM running for 3.199)
2017-11-09 13:32:50.965  WARN 11352 --- [lication Thread] javafx                                   : Loading FXML document with JavaFX API of version 8.0.112 by JavaFX runtime of version 8.0.1012017-11-09 13:32:51.066  INFO 11352 --- [lication Thread] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory2017-11-09 13:32:51.146 DEBUG 11352 --- [lication Thread] org.hibernate.SQL                        : select student0_.id as id1_2_, student0_.contract_number as contract2_2_, student0_.date_of_completion as date_of_3_2_, student0_.date_of_registration as date_of_4_2_, student0_.driving_class as driving_5_2_, student0_.email_address as email_ad6_2_, student0_.first_name as first_na7_2_, student0_.has_criminal_record as has_crim8_2_, student0_.has_identity_card as has_iden9_2_, student0_.is_medically_examined as is_medi10_2_, student0_.is_vip as is_vip11_2_, student0_.last_name as last_na12_2_, student0_.name_of_instructor as name_of13_2_, student0_.number_of_absences as number_14_2_, student0_.number_of_certifications as number_15_2_, student0_.number_of_driving_lessons as number_16_2_, student0_.number_of_extra_lessons as number_17_2_, student0_.occupation as occupat18_2_, student0_.telephone_number as telepho19_2_ from student student0_2017-11-09 13:32:53.695  WARN 11352 --- [lication Thread] javafx                                   : Loading FXML document with JavaFX API of version 8.0.112 by JavaFX runtime of version 8.0.101Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more
Caused by: java.lang.NullPointerException
    at com.rx.drivingschool.controller.AddWindowController.handleOkAction(AddWindowController.java:33)
    ... 58 more

1 个答案:

答案 0 :(得分:0)

在阅读了Tobb的评论后,这似乎是最合乎逻辑的解释,在更多地分析了项目代码后,我看到我有另一个包含ApplicationContextAware实现的类。此类已包含用于初始化FxmlLoader的函数,但它们已硬连线到某个资源。我所要做的就是添加一个方法来设置资源并调用正确的函数。

我会在此处添加代码,以防其他人遇到类似问题或对其有任何用处。

private Object createControllerForType(Class<?> type) {
    return this.applicationContext.getBean(type);
}

private FXMLLoader loadSynchronously(URL resource) throws IllegalStateException {

    FXMLLoader loader = new FXMLLoader(resource);
    loader.setControllerFactory(this::createControllerForType);

    try {
        loader.load();
    } catch (IOException ex) {
        throw new IllegalStateException("Cannot load Resource!", ex);
    }

    return loader;
}

private void initializeFxmlLoader() {
    this.fxmlLoader = loadSynchronously(resource);
    this.presenterProperty.set(this.fxmlLoader.getController());
}

public void setResource (String resource) {
    this.resource = getClass().getResource(resource);
}

public Parent getView() {

    initializeFxmlLoader();

    Parent parent = fxmlLoader.getRoot();
    return parent;
}

这应该足以用Spring初始化任何FXML控制器。

感谢大家花时间回答并帮助我解决这个问题!