<h:selectonemenu> NullPointerException getAsObject方法</h:selectonemenu>

时间:2011-10-01 03:36:05

标签: java jsf-2 primefaces

我正在使用netbean 7.0.1,glassfish 3.1,primefaces 2.2.1。 这是视图

    <ui:define name="content">
            <h:form enctype="multipart/form-data">
                <p:wizard>                      
                    <p:tab title="Tab 01">
                        <p:panel header="This is tab 01">
                            <h:messages errorStyle="color:red"/>
                             <h:panelGrid>
                                 <h:selectOneMenu value="#{sellerController.currentCity}">                                         
                                     <f:selectItems value="#{cityController.listCity}" var="obj" itemLabel="#{obj.name}" itemValue="#{obj}"/>                                                                               
                                </h:selectOneMenu>                                   
                            </h:panelGrid>
                        </p:panel>
                    </p:tab>
                    <p:tab title="Tab 02">
                        <p:panel header="This is tab 02">
                            <h:panelGrid>
                                <p:commandButton value="Save" action="#{sellerController.addProperty()}"/>                                                                          
                            </h:panelGrid>
                        </p:panel>

                    </p:tab>
                </p:wizard>
            </h:form>
        </ui:define>  

也可以尝试使用

    <h:selectOneMenu value="#{sellerController.currentCity}">                                         
        <f:selectItems value="#{cityController.itemsAvailableSelectOne}"/>                                                                                 
    </h:selectOneMenu>  

当我单击选项卡01上的下一个按钮并在选项卡02上保存按钮时,sellerController.currentCity返回空值。保存按钮仍然调用:“addProperty()”。我收到了这个错误

   INFO: java.lang.NullPointerException
   java.lang.NullPointerException
at my.controller.CityController$CityControllerConverter.getAsObject(CityController.java:67)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectOneValue(MenuRenderer.java:202)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:319)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at org.primefaces.component.wizard.Wizard.processValidators(Wizard.java:216)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:508)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1589)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1600)
at javax.faces.component.UIForm.visitTree(UIForm.java:344)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1600)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1600)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1170)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at   com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at   com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

我有两个控制器,这是CityController:

    @Named(value = "cityController")
    @SessionScoped
    public class CityController implements Serializable {

     @EJB
     CityModel cityModel;
     List<City> listCity;    
     /** Creates a new instance of CityController */
     public CityController() {
     }

     public List<City> getListCity(){
     if (listCity==null) {
        listCity = new ArrayList<City>();
        listCity = cityModel.findAll();
     }
     return listCity;
     }

     public SelectItem[] getItemsAvailableSelectMany() {       
        return JsfUtil.getSelectItems(cityModel.findAll(), false);
     }

     public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(cityModel.findAll(), true);
     }

@FacesConverter(forClass = City.class)
public static class CityControllerConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return "";
        }
        CityController controller = (CityController) facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "customerController");
        return controller.cityModel.find(getKey(value));            
    }

    java.lang.Integer getKey(String value) {
        java.lang.Integer key;
        key = Integer.valueOf(value);
        return key;
    }

    String getStringKey(java.lang.Integer value) {
        StringBuffer sb = new StringBuffer();
        sb.append(value);
        return sb.toString();
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof City) {
            City o = (City) object;
            return getStringKey(o.getId());
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + CityController.class.getName());
        }
    }
}

}

这是SellerController

    @Named(value = "sellerController")
    @SessionScoped
    public class SellerController implements Serializable {

@EJB 
CityModel cityModel;

private City currentCity; 

/** Creates a new instance of SellerController */
public SellerController() {

}

public City getCurrentCity() {
    if (currentCity==null) {
        currentCity = new City();
    }
    return currentCity;
}

public void setCurrentCity(City currentCity) {
    this.currentCity = currentCity;
}   

public void addProperty(){
    System.out.println("Call from Save button ?? ");
}
}

这是JsfUtil类

    public class JsfUtil {

public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
    int size = selectOne ? entities.size() + 1 : entities.size();
    SelectItem[] items = new SelectItem[size];
    int i = 0;
    if (selectOne) {
        items[0] = new SelectItem("", "---");
        i++;
    }
    for (Object x : entities) {
        items[i++] = new SelectItem(x, x.toString());
    }
    return items;
}
}

因为CityController。当我使用

 import javax.faces.bean.ManagedBean;
 import javax.faces.bean.SessionScoped;

 @ManagedBean(name = "cityController")
 @SessionScoped

这是工作!

2 个答案:

答案 0 :(得分:1)

考虑到这个“cityController” -

@Named(value = "cityController")
@SessionScoped
public class CityController implements Serializable {

“customerController”正好在下面 -

CityController controller = (CityController) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "customerController");

将其更改为“cityController”,如下所示 -

FacesContext fc = FacesContext.getCurrentInstance();
ELResolver elr = fc.getApplication().getELResolver();
ELContext elc = fc.getELContext();
CityController cc = (CityController) elr.getValue(elc, null, "cityController");

答案 1 :(得分:0)

我认为你的cityModel是NULL。您的EJB注入配置正确吗?