NumberFormatException:null 在数据网格中更改页面时

NumberFormatException:null when change page in datagrid

晚上好,我是 jsf 的新手,我想将属于由视图参数传递的 Marca(品牌)的产品放入数据网格 这是一些 jsf 代码

<f:view>

    <f:metadata>
        <f:viewParam  name="id" value="#{marcaController.id}"/>          
    </f:metadata>
</f:view>

    <ui:composition template="template/template1.xhtml">
    <ui:define name="content1">
        <div class="product card">
    
       <h:form id="form">
       <c:set var="emp" value="#{marcaController.findbyId()}" scope="request" />
            <p:dataGrid var="product" value="#{prodottoController.finbyMarca(emp.idmarca)}" multiViewState="true" columns="3" layout="grid"
                    rows="12" paginator="true" id="cars"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                    rowsPerPageTemplate="6,12,16" >
          
            <f:facet name="header">
                Products for Sale
            </f:facet>

            <div class="product-grid-item card" style="margin-bottom: 0">
                <div class="product-grid-item-top">
                    <div>
                        <i class="pi pi-tag product-category-icon"/>
                        
                    </div>
                    
                </div>
                <div class="product-grid-item-content">
                    <h:graphicImage value="#{product.immagine}" style="width:240px;height:260px" />
                    <div class="product-name">#{product.nome}</div>
                    <div class="product-description">#{product.descrizione}</div>
                    
                </div>
                <div class="product-grid-item-bottom">
                <h:outputText value="#{product.prezzo}" styleClass="product-priceline" style="text-decoration:line-through;margin-left:20px;">
                        <f:convertNumber currencySymbol="€" type="currency"/>
                    </h:outputText>
                    <h:outputText value="#{product.prezzoscontato}" styleClass="product-price" style="color:red;font-size:22px;margin-left:20px;">
                        <f:convertNumber currencySymbol="€" type="currency"/>
                    </h:outputText>
                    <br></br>
                    <div class="brand-name">
                    
                    
                    </div>
                     <br></br>
                    <p:commandButton update=":form:productDetail" oncomplete="PF('productDialog').show()" styleClass="ui-button-warning" 
                                     value="Aggiungi Al Carrello"
                                     icon="pi pi-shopping-cart" style="width:inherit;"
                                    >
                       <f:setPropertyActionListener value="#{product}"
                                                     target="#{prodottoController.selected}"/>
                    </p:commandButton>
                </div>
            </div>

        </p:dataGrid>
     

在马卡控制器中

Marca m=new Marca();
public Integer getId(){
        
        
        
        return m.getIdmarca();
    }
public void setId(Integer i){
    
    
    m.setIdmarca(i);
    
}
    
    public Marca findbyId(){
        
        
        
        
        Integer ident=Integer.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"));
        
        m=marcaDao.findById(ident);
        return m;
    }

它只显示品牌的第一页 如果我更改页面,服务器 运行 此异常:

19:59:15,641 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (default task-3) java.lang.NumberFormatException: null
    at java.base/java.lang.Integer.parseInt(Integer.java:614)
    at java.base/java.lang.Integer.valueOf(Integer.java:983)
    at deployment.esem.war//org.negozio.beans.MarcaController.findbyId(MarcaController.java:93)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javax.el.api@2.0.0.Final//javax.el.ELUtil.invokeMethod(ELUtil.java:245)
    at javax.el.api@2.0.0.Final//javax.el.BeanELResolver.invoke(BeanELResolver.java:338)
    at javax.el.api@2.0.0.Final//javax.el.CompositeELResolver.invoke(CompositeELResolver.java:198)
    at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.parser.AstValue.getValue(AstValue.java:110)
    at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.parser.AstValue.getValue(AstValue.java:177)
    at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:183)
    at org.jboss.weld.core@3.1.3.Final//org.jboss.weld.module.web.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at org.jboss.weld.core@3.1.3.Final//org.jboss.weld.module.web.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:73)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.tag.jstl.core.SetHandler.apply(SetHandler.java:117)
    at javax.faces.api@3.0.0.SP02//javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:55)
    at javax.faces.api@3.0.0.SP02//javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:179)
    at javax.faces.api@3.0.0.SP02//javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:111)
    at javax.faces.api@3.0.0.SP02//javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:55)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.tag.ui.DefineHandler.applyDefinition(DefineHandler.java:65)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:166)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.impl.DefaultFaceletContext$TemplateManager.apply(DefaultFaceletContext.java:372)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.impl.DefaultFaceletContext.includeDefinition(DefaultFaceletContext.java:342)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.tag.ui.InsertHandler.apply(InsertHandler.java:70)
    at javax.faces.api@3.0.0.SP02//javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:55)
    at javax.faces.api@3.0.0.SP02//javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:179)
    at javax.faces.api@3.0.0.SP02//javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:111)
    at javax.faces.api@3.0.0.SP02//javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:55)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:52)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:46)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:271)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:330)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:309)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:162)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:133)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:52)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:46)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:120)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:358)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:266)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:133)
    at javax.faces.api@3.0.0.SP02//javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:101)
    at javax.faces.api@3.0.0.SP02//javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:101)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:181)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
    at com.sun.jsf-impl@2.3.9.SP08//com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
    at javax.faces.api@3.0.0.SP02//javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
    at javax.faces.api@3.0.0.SP02//javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
    at io.opentracing.contrib.opentracing-jaxrs2//io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:52)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletChain.handleRequest(ServletChain.java:68)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow@19.1.0.Final//org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.core@2.1.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.core@2.1.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.core@2.1.0.Final//io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.core@2.1.0.Final//io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.core@2.1.0.Final//io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.core@2.1.0.Final//io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.core@2.1.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow@19.1.0.Final//org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.core@2.1.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow@19.1.0.Final//org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at io.undertow.core@2.1.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletInitialHandler.access0(ServletInitialHandler.java:78)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:133)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:130)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.core.ServletRequestContextThreadSetupAction.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.core.ContextClassLoaderSetupAction.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow@19.1.0.Final//org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create[=13=](SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow@19.1.0.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1541)
    at org.wildfly.extension.undertow@19.1.0.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1541)
    at org.wildfly.extension.undertow@19.1.0.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1541)
    at org.wildfly.extension.undertow@19.1.0.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1541)
    at org.wildfly.extension.undertow@19.1.0.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1541)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletInitialHandler.access[=13=]0(ServletInitialHandler.java:78)
    at io.undertow.servlet@2.1.0.Final//io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(ServletInitialHandler.java:99)
    at io.undertow.core@2.1.0.Final//io.undertow.server.Connectors.executeRootHandler(Connectors.java:370)
    at io.undertow.core@2.1.0.Final//io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:830)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1982)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at org.jboss.threads@2.3.3.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at java.base/java.lang.Thread.run(Thread.java:834)

它不维护状态,它会在每个页面更改时更改 id

为什么? 最重要的是谢谢!

当您调用 Integer.valueOf(null) 时,您将获得 NumberFormatException

您应该在 findbyId() 方法中检查 null,具体取决于您的业务逻辑。

为了更好地理解 <f:vievParam> 请阅读此内容:

可以用来做什么