简体   繁体   中英

Can't instantiate class, while populating selectonemenu in JSF2

I'm trying to populate selectOneMenu from DB and I get run time error Can't instantiate class . I get

java.lang.NullPointerException at pointControllerConverter.getPoints(pointControllerConverter.java:89)

Any advice?

This is my code:

xhtml file:

    <p:selectOneMenu id="point" value="#{pointController.selectedPoint}"
                     var="items_var"> 

         <f:selectItems value="#{pointController.points}" var="points_var" 
                        itemLabel="#{points_var.name}" itemValue="#{points_var.name}"/>

    </p:selectOneMenu>

Managed bean:

    @ManagedBean(name = "pointController")
    @SessionScoped

    public class PointController implements Serializable {

    @EJB
        private ethoam.entity.PointFacade ejbFacade;
        private List<point> points;
        private point selectedPoint;
        private point[] selectedPoints;
        private List<Point> selectedPointsList;
        private SelectItem[] PointNamesOptions;

        public PointController() {
                  this.initList();
        }

        @PostConstruct
        private void initList() {

        points = new ArrayList<point>(PointControllerConverter.getPoints().values());


    }

        // getters setters
     }

Converter class:

    public static class PointControllerConverter implements Converter {

    @PersistenceContext(unitName = "PointPU")
    private static EntityManager ejbfacade;

    public static Map<String, point> points = new HashMap<String, point>();

    public static Map<String,point> getPoints() {

    points = (Map<String, point>) ejbfacade.createNamedQuery("SELECT d FROM point d WHERE d.name = :name");
    return points;
    }

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        PointController controller = (PointController) facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "pointController");
        return controller.ejbFacade.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) {
        StringBuilder sb = new StringBuilder();
        sb.append(value);
        return sb.toString();
    }

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

}

Well first I don't know why you are using a Converter as a List supplier !

You might need to move getPoints() logic into a ManagedBean.

On the other hand the NullPointerException is caused due the failed injection of of the entity manager (which I don't know why it's a static).

In a Converter or Validator you can't inject using @PersistenceContext or @EJB

See Why: How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?

You may do this:

Context envCtx = InitialContext().lookup("java:comp/env");
EntityManager em =  (EntityManager) envCtx.lookup("persistence/PointPU");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM