【问题标题】:[vaadin]nullPointerExecption when tring to use service in view constructor[vaadin] 尝试在视图构造函数中使用服务时出现空指针异常
【发布时间】:2021-05-19 11:22:31
【问题描述】:

我是 vaadin 流的新手。我正在尝试按照网站上提供的教程创建列表视图。 https://vaadin.com/docs/v14/flow/tutorials/in-depth-course/configuring-vaadin-grid

@Route("")
public class MainView extends VerticalLayout {

    private ContactService contactService;
    private Grid<Contact> grid = new Grid<>(Contact.class);

    public MainView(ContactService contactService) {
        this.contactService = contactService; 
        addClassName("list-view");
        setSizeFull();
        configureGrid();

        add(grid);
        updateList(); 
    }

    private void configureGrid() {
        grid.addClassName("contact-grid");
        grid.setSizeFull();
        grid.setColumns("firstName", "lastName", "email", "status");
    }

    private void updateList() {
        grid.setItems(contactService.findAll());
    }

}

在这里,当我执行 updateList() 方法时,我在服务实例上获得了 nullPointerExecption。我尝试使用 repository.findAll() 但面临同样的问题。

为了检查服务中是否存在问题,我创建了 REST API 并在这里调用了相同的服务方法,我得到了正确的结果。请帮忙。

这是我遇到的错误。

There was an exception while trying to navigate to '' with the exception message 'Error creating bean with name 'com.example.demo.ui.MainView': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.demo.ui.MainView]: Constructor threw exception; nested exception is java.lang.NullPointerException'

【问题讨论】:

  • 听起来你得到一个空服务作为参数。您能否也展示一下您是如何生成 MainView 的?
  • 你能告诉我们错误的实际堆栈跟踪吗?
  • 嗨,安娜,是的,我在代码中为这个地方获得了空服务。 There was an exception while trying to navigate to '' with the exception message 'Error creating bean with name 'com.example.demo.ui.MainView': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.demo.ui.MainView]: Constructor threw exception; nested exception is java.lang.NullPointerException' 这是完整的堆栈跟踪
  • 您的目录结构是什么样的?包树中SpringBootApplication下面的视图吗?
  • 我的意思是您可以在 MainView 之外的某个地方添加代码吗?您正在创建 MainView 的地方?

标签: vaadin vaadin-flow


【解决方案1】:

使用@Service 注释您的 ContactService 实现?

【讨论】:

  • 谢谢早些时候我在这个类中使用@Component。我也使用成员依赖注入而不是参数注入。所以这两个都解决了我的问题。
猜你喜欢
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多