简体   繁体   English

JavaFX Spring Boot @Autowire服务为空

[英]JavaFX Spring Boot @Autowire Service is Null

I get a java.lang.NullPointerException at AddWindowController.java:33 whenever i try to access the drivingSchoolService field and I can't, for the life of me, figure out what's wrong. 每当我尝试访问DrivingSchoolService字段时,我都会在AddWindowController.java:33处收到java.lang.NullPointerException ,而我一生都无法找出问题所在。 I've autowired the same field in another controller and it works perfectly fine, but it won't work here for some reason. 自动装配相同的字段在另一个控制器,它工作完全正常,但它不会在这里工作的某些原因。 And since both classes are in the same package I don't think it's a scanning issue. 而且由于这两个类都在同一个程序包中,所以我认为这不是扫描问题。

Any help would be greatly appreciated. 任何帮助将不胜感激。

AddWindowController: 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 (no errors here even though the code is practically the same) 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: 服务:

@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);

}

Service Implementation: 服务实施:

@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);
 }
}

Stack Trace: 堆栈跟踪:

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

After reading Tobb's comment, it seemed the most logical explanation and after analyzing the project code more, I saw I had another class which contained an ApplicationContextAware implementation. 在阅读Tobb的评论之后,这似乎是最合乎逻辑的解释,并且在对项目代码进行了更多分析之后,我看到我还有另一个包含ApplicationContextAware实现的类。 This class already contained functions for initializing the FxmlLoader, however they were hardwired to a certain resource. 此类已经包含用于初始化FxmlLoader的函数,但是它们已硬连线至特定资源。 All I had to do was add a method to set the resource and call the right function. 我要做的就是添加一个方法来设置资源并调用正确的函数。

I will add the code down here in case anyone else runs into a similar problem or has any use for it. 如果有人遇到类似问题或有其他用途,我将在此处添加代码。

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;
}

This should be enough to initialize any FXML controllers with Spring. 这足以用Spring初始化任何FXML控制器。

Thank you all for taking the time to answer and helping me solve this problem! 谢谢大家花时间回答并帮助我解决了这个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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