0

I have a table Users and Specializations in database

Users:

CREATE  TABLE IF NOT EXISTS `ePrzychodnia`.`users` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `firstName` VARCHAR(45) NOT NULL ,
  `lastName` VARCHAR(45) NOT NULL ,
  `specialization_id` INT NULL ,
  PRIMARY KEY (`id`) ,
  UNIQUE INDEX `personalId_UNIQUE` (`personalId` ASC) ,
  INDEX `fk_users_specializations1_idx` (`specialization_id` ASC) ,
  CONSTRAINT `fk_users_specializations1`
    FOREIGN KEY (`specialization_id` )
    REFERENCES `ePrzychodnia`.`specializations` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Specializations:

CREATE  TABLE IF NOT EXISTS `ePrzychodnia`.`specializations` (
  `id` INT NOT NULL ,
  `name` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`id`) ,
  UNIQUE INDEX `name_UNIQUE` (`name` ASC) )
ENGINE = InnoDB;

Entity class:

Users:

public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "firstName")
    private String firstName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "lastName")
    private String lastName;
    @Basic(optional = false)
    @JoinColumn(name = "specialization_id", referencedColumnName = "id")
    @ManyToOne
    private Specialization specializationId;

Specialization:

public class Specialization implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    private String name;
    @OneToMany(mappedBy = "specializationId")
    private Collection<User> userCollection;

and I try create converter to one select menu in JSF:

SelectOneMenu:

                    <h:outputLabel for="specialization" value="#{msg.specialization}"/>
                    <p:selectOneMenu id="specialization" value="#{userMB.user.specializationId}" effect="fade" style="width:200px" converter="specializationConverter">
                        <f:selectItems value="#{specializationMB.allSpecialization}" var="specialization" itemValue="#{specialization}" itemLabel="#{specialization.name}"/>
                    </p:selectOneMenu>
                    <p:message for="specialization"/>

and Converter:

  public class SpecializationConverter implements Converter{

        private SpecializationDao specializationDao = new SpecializationDao();

        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            Integer id = Integer.parseInt(value);
            return specializationDao.find(id);
        }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            return ((Specialization) value).getId().toString();
        }

}

But convertert dont work;/ I have a error:

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
    at pl.Project.dao.SpecializationDao.find(SpecializationDao.java:36)
    at pl.Project.converter.SpecializationConverter.getAsObject(SpecializationConverter.java:29)

SpecializationDao:

@Stateless
public class SpecializationDao implements SpecializationDaoLocal {

    @PersistenceContext
    private EntityManager em;
    private Specialization specialization;

    public SpecializationDao() {
    }

    public SpecializationDao(Specialization specialization) {
        this.specialization = specialization;
    }

    @Override
    public Specialization find(int specializationId) {
        return em.find(Specialization.class, specializationId);
    }

    @Override
    public List<Specialization> findAllSpecialization() {
        Query q = em.createNamedQuery("Specialization.findAll");
        List<Specialization> specializations = q.getResultList();
        return specializations;
    }
}
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
The Nightmare
  • 631
  • 3
  • 16
  • 34
  • 2
    **Read the stacktrace**. There's a `null` variable in your `specializationDao.find` method. Which one could be? It's up to you and a debugger to find it. By the way, adding all the info of the database tables, JPA entities and your converter isn't related to the problem, and this **is not** a JSF problem. – Luiggi Mendoza Jul 02 '13 at 17:11
  • 1
    @Orange91 You posted so many things but missed `SpecializationDao` !!! – NINCOMPOOP Jul 02 '13 at 17:15
  • 3
    possible duplicate of [JSF2, can I use @EJB to inject a service into a @FacesConverter?](http://stackoverflow.com/questions/7665673/jsf2-can-i-use-ejb-to-inject-a-service-into-a-facesconverter). By the way, the problem is caused because you're manually creating the EJB instead of injecting it, so the `EntityManager em` won't be created thus being `null` and giving you the NPE. – Luiggi Mendoza Jul 02 '13 at 17:19
  • I added specializationDao. @BalusC: What's with parse null to int? – The Nightmare Jul 02 '13 at 17:19
  • 1
    @Luiggi: not being able to use `@EJB` in a `@FacesConverter` is actually a true JSF problem. The OP simply attempted to solve it the wrong way, hereby not making it a JSF problem anymore :) – BalusC Jul 02 '13 at 17:19
  • 1
    @BalusC yes, but only after posting the code of `SpecializationDao` class this could be understood :). – Luiggi Mendoza Jul 02 '13 at 17:20
  • 1
    @Orange: your root problem is that you manually created the EJB instance the basic Java way using `new` instead of letting the Java EE container create and manage it. The `new` operator doesn't trigger `@PersitenceContext` and friends at all. The `em` ends up being `null`. Look once again at the stack trace. It refers to the line where `em` is the only possible variable which would cause the NPE by being `null`. – BalusC Jul 02 '13 at 17:21
  • 2
    @Luiggi: yes, now he actually posted the code behind the 1st line of the stacktrace, it's become much more clear. Orange: it's time to learn how to interpret stacktraces (and [read javadocs](http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html)) This is in turn indeed basic Java, not JSF related. – BalusC Jul 02 '13 at 17:22
  • 1
    To solve the problem, check the possible duplicate link I've posted to your question showing 3 ways to solve it by BalusC. – Luiggi Mendoza Jul 02 '13 at 17:24
  • Ok Luiggi Mendoza your link helped me. I must make my converter managedbean. All work. – The Nightmare Jul 02 '13 at 17:27

0 Answers0