这段代码有什么问题?它抛出空错误

what is wrong in this code? it is throwing the null error

package com.vaadin.ui.view;

public class AddConsumerView extends FormLayout{

private ConsumerUI consumerUI;

@Autowired
private ConsumerServiceInterface consumerServiceInterface;

//horizontal layout for the id's
private HorizontalLayout consumerIDLayout = new HorizontalLayout();
private TextField consumer_id = new TextField("Consumer Id");
private TextField household_id = new TextField("Household Id");
private TextField legal_hold = new TextField("Legal Hold");
private TextField deceased_fg = new TextField("Deceased");
private DateTimeField deceased_dt = new DateTimeField("Deceased DateTime");


//Horizontal layout for the INS
private HorizontalLayout consumerINSLayout = new HorizontalLayout();
private TextField ins_pqid = new TextField("Ins PQID");
private TextField ins_rid = new TextField("Ins RID");
private TextField ins_efid = new TextField("Ins EFID");
private DateTimeField ins_tmstmp = new DateTimeField("Ins Date time");

// Horizontal layout for the UPD
private HorizontalLayout consumerUPDLayout = new HorizontalLayout();
private TextField upd_pqid = new TextField("Upd PQID");
private TextField upd_rid= new TextField("Upd RID");
private TextField upd_efid = new TextField("Upd EFID");
private DateTimeField upd_tmstmp = new DateTimeField("Upd Date time");
private Consumer consumerData = new Consumer();

//Buttons
private Button saveConsumerButton =  new Button("save"); 

//binding the data to the field
private Binder<Consumer> consumerBinder = new Binder<>(Consumer.class);





public AddConsumerView(ConsumerUI consumerUI) {
    this.consumerUI = consumerUI;
    consumerIDLayout.addComponents(consumer_id, household_id, legal_hold, deceased_dt,deceased_fg);
    consumerINSLayout.addComponents(ins_pqid, ins_rid, ins_efid, ins_tmstmp);
    consumerUPDLayout.addComponents(upd_pqid,upd_rid, upd_efid, upd_tmstmp);
    VerticalLayout vertical = new VerticalLayout();
    vertical.addComponents(consumerIDLayout, consumerINSLayout, 
    consumerUPDLayout,saveConsumerButton);
    //addComponent(vertical);
    bindFields();
    consumerBinder.setBean(new Consumer());
    saveConsumerButton.setStyleName("primary");
    saveConsumerButton.addClickListener(e->saveConsumer());
    addComponents(vertical);
}


public void saveConsumer() {


    //consumerBinder.bindInstanceFields(this);

    System.out.println(consumerBinder.getBean().toString());
    consumerServiceInterface.addConsumer(consumerBinder.getBean());
}

public void bindFields() {

consumerBinder.forField(consumer_id)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getConsumer_id,Consumer::setConsumer_id);

consumerBinder.forField(legal_hold)
    .bind(Consumer::getLegal_hold, Consumer::setLegal_hold);

consumerBinder.forField(deceased_fg)
    .bind(Consumer::getDeceased_fg, Consumer::setDeceased_fg);

consumerBinder.forField(household_id)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getHousehold_id,Consumer::setHousehold_id);

consumerBinder.forField(ins_efid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getIns_efid,Consumer::setIns_efid);

consumerBinder.forField(ins_pqid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getIns_pqid,Consumer::setIns_pqid);

consumerBinder.forField(ins_rid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getIns_rid,Consumer::setIns_rid);

consumerBinder.forField(upd_efid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getUpd_efid,Consumer::setUpd_efid);

consumerBinder.forField(upd_pqid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getUpd_pqid,Consumer::setUpd_pqid);

consumerBinder.forField(upd_rid)
    .withConverter(new StringToLongConverter("Must be a number"))
    .bind(Consumer::getUpd_rid,Consumer::setUpd_rid);

consumerBinder.forField(upd_tmstmp)
    .withConverter(new StringTimestampConvertor())
    .bind(Consumer::getUpd_tmstmp, Consumer::setUpd_tmstmp);

consumerBinder.forField(ins_tmstmp)
    .withConverter(new StringTimestampConvertor())
    .bind(Consumer::getIns_tmstmp, Consumer::setIns_tmstmp);

consumerBinder.forField(deceased_dt)
    .withConverter(new StringTimestampConvertor())
    .bind(Consumer::getDeceased_dt, Consumer::setDeceased_dt);

consumerBinder.forField(upd_tmstmp)
    .withConverter(new StringTimestampConvertor())
    .bind(Consumer::getUpd_tmstmp, Consumer::setUpd_tmstmp);


    }

}

StringTimestampConvertorClass

package com.vaadin.convertor;


    @SuppressWarnings("serial")
    public class StringTimestampConvertor implements Converter<LocalDateTime, Timestamp> {

    @SuppressWarnings("unchecked")

    public Result<Timestamp> convertToModel(LocalDateTime value, ValueContext context) {
        System.out.println("check"+value);
        Result<Timestamp> rs =  Result.ok(Timestamp.valueOf(value));
        return rs;
    }

    @Override
    public LocalDateTime convertToPresentation(Timestamp value, ValueContext context) {
        // TODO Auto-generated method stub
        return null;
    }

}

sysout 正在工作,但 saveConsumer 函数给出空错误并且 stringtimestampconvertor 被 运行 多次。

java.lang.NullPointerException: null at com.vaadin.ui.view.AddConsumerView.saveConsumer(AddConsumerView.java:82) ~[classes/:na] at com.vaadin.ui.view.AddConsumerView.lambda[=14=](AddConsumerView.java:71) ~[classes/:na] at com.vaadin.ui.view.AddConsumerView$$Lambda01/954546734.buttonClick(Unknown Source) ~[na:na] at sun.reflect.GeneratedMethodAccessor433.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_45] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_45] at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510) ~[vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:211) ~[vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:174) ~[vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1029) ~[vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.ui.Button.fireClick(Button.java:370) ~[vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.ui.Button.click(Button.java:57) ~[vaadin-server-8.0.0.jar:8.0.0] at sun.reflect.GeneratedMethodAccessor432.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_45] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_45] at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:155) ~[vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:116) ~[vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:443) [vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:415) [vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:274) [vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:90) [vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41) [vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1464) [vaadin-server-8.0.0.jar:8.0.0] at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:380) [vaadin-server-8.0.0.jar:8.0.0] at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.29.jar:8.5.29] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.29.jar:8.5.29] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_45] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_45] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.29.jar:8.5.29] at java.lang.Thread.run(Unknown Source) [na:1.8.0_45]

一切正常,除了在提交表单时 saveConsumer 函数 sysout 语句工作正常 consumerServiceInterface.saveConsumer(conusmerBinder.getBean()) 抛出空错误

编辑:

您能否验证您的 ConsumerServiceInterface 实例不为空?您没有在视图上使用 @Route,那么您如何在应用程序中使用此视图?如果它是通过构造函数发生的,那么 Spring 不会自动装配字段。 How does autowiring work in Spring


根据您之前在此处提出的问题: 我假设您在

获得 NPE

Result<Timestamp> rs = Result.ok(Timestamp.valueOf(value));

这是预期的,如果通过 value 则为空 JavaDocs: Timestamp valueOf。您应该在那里添加一个适当的空检查,并在此基础上进一步

此外,您为什么要返回任何 Timestamp 一个 null LocalDateTime

你应该这样做 Datamodel Conversion:

class MyConverter implements Converter<String, Integer> {
  @Override
  public Result<Integer> convertToModel(String fieldValue, ValueContext context) {
    // Produces a converted value or an error
    try {
      // ok is a static helper method that creates a Result
      return Result.ok(Integer.valueOf(fieldValue));
    } catch (NumberFormatException e) {
      // error is a static helper method that creates a Result
      return Result.error("Please enter a number");
    }
  }

  @Override
  public String convertToPresentation(Integer integer, ValueContext context) {
    // Converting to the field type should always succeed,
    // so there is no support for returning an error Result.
    return String.valueOf(integer);
  }
}

除非您通过 Spring 创建表单,否则您将无法使用 @Autowired。永远不要使用 @Autowired 字段,总是使用基于构造函数的注入,这样你会更快遇到这种问题。

然后你被迫

  • 手动构建并传递所需的对象(你可以在链中更高的地方做什么,事物参与 Spring 机制(例如 Vaadin Views))
  • 或者您被迫自己使用 Spring 机制(例如,从应用程序上下文中获取一个 bean)

我建议看看 docs