开发者

Null Pointer exception while getting request parameters

<h:dataTable id="dt1" value="#{StudentMark.stuList}" var="stuList"  bgcolor="#9AC8E6" border="10" cellpadding="5" cellspacing="3" rows="18" width="120%" dir="LTR" frame="hsides>
                <h:column>
                    <f:facet name="header">
                        <h:outputText style=""value="Student Number" />
                    </f:facet>
                    <h:outputText style="" value="#{stuList.stuNumber}"></h:outputText>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Date"/>
                    </f:facet>
                    <h:outputText value="#{stuList.date}">
                        <f:convertDateTime type="date" pattern="dd-MM-yyyy"/></h:outputText>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Name"/>
                    </f:facet>
                    <h:outputText value="#{stuList.stuName}"></h:outputText>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Division"/>
                    </f:facet>
                    <h:outputText value="#{stuList.division}"></h:outputText>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Annual Marks"/>
                    </f:facet>
                    <h:outputText value="#{stuList.annualMark}"></h:outputText>
                </h:column>                    
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Student History"/>
                    </f:facet>
                     <h:form>
                        <h:commandButton id = "historyBtn" value="Student History" action="#{stuBean.showHistory}">
                            <f:param name="sNumber" value="#{stuBean.stuNumber}" />
                            <f:param name="sName" value="#{stuBean.stuName}" />
                        </h:commandButton></h:form>                        
                </h:column>
  开发者_StackOverflow          </h:dataTable>

When i try to get the parameters passed using

        FacesContext context = FacesContext.getCurrentInstance();
        Map requestMap = context.getExternalContext().getRequestParameterMap();
        String studentNum = (String) requestMap.get("sNumber");
        String studentName = (String) requestMap.get("sName");

its showing "null" in studentName and studentNum thereby resulting with a null pointer exception. Any ideas to resolve this???


I've resolved my issue. The mistake made was I've trying to pass the parameters at the page load itself,tried making the param value as value="#{stuList.stuNumber}" and got the error resolved.

<h:form>
 <h:commandLink id = "historyBtn" value="Student History" action="#{stuBean.showHistory}">
     <f:param name="sNumber" value="#{stuList.stuNumber}" />
     <f:param name="sName" value="#{stuList.stuName}" />
  </h:commandLink></h:form> 

Thanks for all the professionals for their time.


You are mixing POST and GET requests. h:form and h:commandbutton are for POST requests. f:param is for GET requests. If you want a GET request use h:button or h:link

For a quick overview of GET support look here it also explains how you can bind the parameter to a bean without having to go to the request parameter map.


I think f:param works only in h:commandLink and h:outputLink but not in h:commandButton.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜