java - vaadin 表中出现 NullPointerException?

标签 java vaadin vaadin7

我在使用 vaadin 表时遇到问题。当我更改显示的值行时,返回 NullPointerException,我不知道为什么。在vaadin book Chapter5 Table中,当这个问题发生时,有一个方法setNullSelectionAllowed(false),但对我来说仍然不起作用。

// Table configs
Table tabela = new Table();
tabela.setSizeFull();       
tabela.addContainerProperty("Aperfeiçoamento", String.class, null);
tabela.addContainerProperty("Entidade", String.class, null);
tabela.addContainerProperty("Início", String.class, null);
tabela.addContainerProperty("Conclusão", String.class, null);   
tabela.setNullSelectionAllowed(false);
tabela.setImmediate(true);
tabela.setSelectable(true);
tabela.setColumnReorderingAllowed(false);
tabela.addValueChangeListener(this);

@Override
public void valueChange(ValueChangeEvent event) {
    /** preenche os campos do formulario com o id do aperfeicoamento */
    String id = event.getProperty().getValue().toString();
    List<Aperfeicoamento> lista = new AperfeicoamentoDAO().getAperfeicoamentoById(Integer.parseInt(id));
    if(!lista.isEmpty()){
        try{
            for(Aperfeicoamento apf : lista){
                aperfeicoamento.setValue(apf.getAperfeicoamento());
                entidadeEnsino.setValue(apf.getEntidadeEnsino());
                cidade.setValue(apf.getCidade());
                comboEstado.setValue(apf.getEstado());
                inicio.setValue(new SimpleDateFormat("dd/MM/yyyy").parse(apf.getInicio()));
                conclusao.setValue(!apf.getConclusao().isEmpty() ? new SimpleDateFormat("dd/MM/yyyy").parse(apf.getConclusao()) : null);    
            }
        }catch(ParseException e){
            e.printStackTrace();
        }
    }
}

// DAO
/** retorna uma lista de perfeicoamento por id e cpf */
public List<Aperfeicoamento> getAperfeicoamentoById(Integer id){
    List<Aperfeicoamento> lista = new ArrayList<Aperfeicoamento>();     
    try{
        PreparedStatement stm = this.con.prepareStatement("SELECT * FROM aperfeicoamento WHERE idAperfeicoamento = ? AND cpf = ?");         
        stm.setInt(1, id);
        stm.setString(2, SessionCurriculum.getCpfInSession());
        ResultSet rs = stm.executeQuery();
        if(rs.next()){
            Aperfeicoamento apf = new Aperfeicoamento();
            apf.setIdAperfeicoamento(rs.getInt("idAperfeicoamento"));
            apf.setAperfeicoamento(rs.getString("aperfeicoamento"));
            apf.setEntidadeEnsino(rs.getString("entidadeensino"));
            apf.setCidade(rs.getString("cidade"));
            apf.setEstado(rs.getString("estado"));
            apf.setInicio(new ControlaDatas().getDataFormatada(rs.getString("inicio")));
            apf.setConclusao(rs.getString("conclusao") == null ? null : new ControlaDatas().getDataFormatada(rs.getString("conclusao")));
            lista.add(apf);
        }
        rs.close();
        stm.close();
    }catch(SQLException e){
        new Notification("Erro tentando select em aperfeiçoamento <br/>", e.getLocalizedMessage(), Notification.Type.ERROR_MESSAGE, true).show(Page.getCurrent());          
    }
    return lista;
}
com.vaadin.event.ListenerMethod$MethodException: Invocation of method valueChange in br.ind.ibg.curriculunsibg.views.CursosView

failed. at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:528) at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:167) at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:969) at com.vaadin.ui.AbstractField.fireValueChange(AbstractField.java:1126) at com.vaadin.ui.AbstractField.setValue(AbstractField.java:542) at com.vaadin.ui.AbstractSelect.setValue(AbstractSelect.java:702) at com.vaadin.ui.AbstractSelect.changeVariables(AbstractSelect.java:521) at com.vaadin.ui.Table.changeVariables(Table.java:2880) at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:396) at com.vaadin.server.communication.ServerRpcHandler.handleBurst(ServerRpcHandler.java:221) at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:111) at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:91) at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37) at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371) at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:724) Caused by: java.lang.NullPointerException at br.ind.ibg.curriculunsibg.views.CursosView.valueChange(CursosView.java:304) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508) ... 27 more

有什么想法吗?

最佳答案

当您编写 setNullSelectionAllowed(false) 时,并不意味着总会选择某些项目。 在您的情况下,为了避免 NPE:

  • 使用 setNullSelectionItemId() 设置一些默认值
  • 使用 if(event.getProperty().getValue() != null) 检查是否选择了某个项目

关于java - vaadin 表中出现 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21020997/

相关文章:

java - 验证代码是否在 Vaadin 7 应用程序的用户界面线程上运行

java - Vaadin 7 应用程序中推送的最小示例 ("@Push")

java - 在 Vaadin 7 中,将数据/参数传递给 BrowserWindowOpener 的新 UI 实例?

eclipse - Eclipse/Tomcat发布不必要/有问题的依赖项

java - Http错误响应: internal server error

java - 在 JBoss 5 上更改 war 文件中的 HTML、JS 和 CSS 等静态内容,无需重新部署

java - 配置 proguard-maven-plugin 过滤库 jar 的 META-INF

java - 体验 Vaadin touchkit

java - 使用 LayoutClickListener 终止替换组件

java - 写入一个 header ,以便段默认名称也根据最大出现次数打印