Lazy DataTable的行选择事件对象始终为空。

3
我正在尝试使用 p:datatable 显示之前的搜索结果,并选择一行以在显示对话框上显示(如展示中所示:http://www.primefaces.org/showcase/ui/data/datatable/lazy.xhtml)。但是 ajax 事件 "rowSelect" 的 SelectEvent.getObject() 总是返回 null,当前行永远未被选中。

以下是堆栈跟踪:

Caused by: java.lang.NullPointerException
    at com.gestion.projet.web.jsf.ProjetComponentImpl.onRowSelect(ProjetComponentImpl.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy58.onRowSelect(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy10.onRowSelect(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    ... 38 more

这是我的XHTML文件:
<p:dataTable var="current" value="#{ProjetComponent.lazyModel}" 
                paginator="true" rows="10" 
                paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                rowsPerPageTemplate="5,10,15" selectionMode="single"
                selection="#{ProjetComponent.projet}" id="projetTable" lazy="true">
                <p:ajax event="rowSelect" listener="#{ProjetComponent.onRowSelect}"
                    update=":form:projetDetail" oncomplete="PF('projetDialog').show()" />
                <p:column headerText="Id" sortBy="#{current.idprojet}" filterBy="#{current.idprojet}">
                    <h:outputText value="#{current.idprojet}" />
                </p:column>
                <p:column headerText="Nom" sortBy="#{current.nomprojet}"
                    filterBy="#{current.nomprojet}">
                    <h:outputText value="#{current.nomprojet}" />
                </p:column>

            </p:dataTable>

这是在ProjetComponent组件中的onRowSelect方法,其中包含第92行:

@Scope("session")
@Component("ProjetComponent")

public class ProjetComponentImpl implements ProjetComponent {

    private Projet projet;

    @Autowired
    private ProjetService projetService;

    private LazyDataModel<Projet> lazyModel;
    /** 
     */
    public ProjetComponentImpl() {
    }





    @PostConstruct
        public void init() {
            //initialize the data here
            this.phases = new Phase();
            //similar for other fields
            this.utilisateurs =new Utilisateur();
            this.projet=new Projet();
            this.lazyModel = new LazyProjetDataModel(projetService.findAllProjets(-1, -1));
        }

    @Transactional
     public LazyDataModel<Projet> getLazyModel() {
        this.lazyModel = new LazyProjetDataModel(projetService.findAllProjets(-1, -1));

        return lazyModel;
    }

     @Transactional
    public void setLazyModel(LazyDataModel<Projet> lazyModel) {
        this.lazyModel = lazyModel;
    }
@Transactional
    public void onRowSelect(SelectEvent event) {
    if (event != null){

            FacesMessage msg = new FacesMessage("Projet Selected", ((Projet) event.getObject()).getIdprojet().toString());//Line 92
            FacesContext.getCurrentInstance().addMessage(null, msg);
    }   }
}

这是 LazyProjetDataModel

public class LazyProjetDataModel extends LazyDataModel<Projet>  {
    private static final long serialVersionUID = 1L;
    private List<Projet> datasource;

    public LazyProjetDataModel(List<Projet> datasource) {
        this.datasource = datasource;
    }

    @Override
    public Projet getRowData(String rowKey) {
        for(Projet projet : datasource) {
            if(projet.getIdprojet().equals(rowKey))
                return projet;
        }

        return null;
    }

    @Override
    public Object getRowKey(Projet projet) {
        return projet.getIdprojet();
    }
    @Override
    public List<Projet> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        List<Projet> data = new ArrayList<Projet>();

        //filter
        for(Projet car : datasource) {
            boolean match = true;

            if (filters != null) {
                for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                    try {
                        String filterProperty = it.next();
                        Object filterValue = filters.get(filterProperty);
                        String fieldValue = String.valueOf(car.getClass().getField(filterProperty).get(car));

                        if(filterValue == null || fieldValue.startsWith(filterValue.toString())) {
                            match = true;
                    }
                    else {
                            match = false;
                            break;
                        }
                    } catch(Exception e) {
                        match = false;
                    }
                }
            }

            if(match) {
                data.add(car);
            }
        }

        //sort
        if(sortField != null) {
       //     Collections.sort(data, new LazySorter(sortField, sortOrder));
        }

        //rowCount
        int dataSize = data.size();
        this.setRowCount(dataSize);

        //paginate
        if(dataSize > pageSize) {
            try {
                return data.subList(first, first + pageSize);
            }
            catch(IndexOutOfBoundsException e) {
                return data.subList(first, first + (dataSize % pageSize));
            }
        }
        else {
            return data;
        }
    }
}

这是数据表的截图: enter image description here 当我选择一行时,我得到的结果如下图所示: enter image description here

2个回答

1

你的问题是这个方法返回了空值(null)

 @Override
    public Projet getRowData(String rowKey) {
        for(Projet projet : datasource) {
            if(projet.getIdprojet().equals(rowKey))
                return projet;
        }

        return null;
    }

删除它并尝试


不要删除所有的 getRowData 方法,尝试修改。 - sghaier ali
我没有任何堆栈跟踪异常,但问题仍然存在,我再次得到第二个截图。 - Ayman S
不要删除该方法中的代码,删除整个方法。 - sghaier ali
让我们在聊天中继续这个讨论 - Ayman S
@Override public Object getRowKey(Projet projet) { return projet.getIdprojet(); } 请删除 - sghaier ali
显示剩余2条评论

0

你好,

你的错误是:

Caused by: java.lang.NullPointerException
at com.gestion.projet.web.jsf.ProjetComponentImpl.onRowSelect(ProjetComponentImpl.java:92)

是由...引起的

((Projet) event.getObject()).getIdprojet().toString()

您的对象项目不为空。空对象是idProject。 请前往您的项目并验证其是否有赋值的ID。


什么是id已赋值的含义? - Ayman S
valorized:不是空的,具有非空值。做到这一点: 将此更改为:((Projet) event.getObject()).getIdprojet().toString(); 使用以下代码替换:Projet j = (Projet) event.getObject(); System.out.println(p.getIdprojet()); p.getIdprojet().toString(); 并放置您的堆栈跟踪错误。 - sghaier ali
改成什么?我试着改成nomProjet,但是问题还是一样。 - Ayman S
你可以使用以下代码读取我写的内容:Projet j = (Projet) event.getObject(); System.out.println(j.getIdprojet()); p.getIdprojet().toString(); - sghaier ali
我在这一行遇到了空指针异常:System.out.println(j.getIdprojet()); - Ayman S
等一下,如果我理解正确的话,您的项目为空,请在页面上放置一张图片(我想看看您的表是否有行)。 - sghaier ali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接