I have the following code which is resulting in a java.lang.NullPointerException
:
List results = em.createNamedQuery("User.findByUserName").setParameter("userName", username).getResultList();
User is the entity bean, which contains:
@NamedQuery(name = "User.findByUserName", query = "SELECT u FROM User u WHERE u.userName = :userName"),
Does anyone know why?
Try making it a Stateless Bean by adding @Stateless annotation above the public class. Worked for me.
This can come about when your psql is referencing an entity property name. I was doing something like
:param.user = x.user
in my psql, but trying to grab the property "user" from a param (which hasn't been passed in yet when createQuery runs) was giving me the NPE. I wish this sort of thing were possible, though..A method
getResultList()
doesn't throwNullPointerException
(it returns empty list if there's no match), so I'm guessing that it should be one of the following:EntityManager em
(did you forget@PersistenceContext
annotation?)username
is nullIn case of EntityManager null, check whether you have persistence.xml file (it's mandatory!). It should look like: