Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:28:27+00:00 2026-06-13T21:28:27+00:00

I think that the question is simple, I get the error: Caused by: java.lang.NullPointerException

  • 0

I think that the question is simple, I get the error:

Caused by: java.lang.NullPointerException
at co.edu.unal.bienestar.dao.UserDao.save(UserDao.java:27)
at co.edu.unal.bienestar.facade.UserFacade.createUser(UserFacade.java:18)
at co.edu.unal.bienestar.mb.UserMB.createUser(UserMB.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 27 more

When I try the create a new user, with a application which only have a entity class:

@Entity
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String username;
    private String password;
    private Role role;

    <here getters and setters>
}

a Data Access Object:

public class UserDao {

    @PersistenceContext
    private EntityManager em;


    public void save(User user) {
        em.persist(user);
    }

    public void delete(User user) {
        User userToBeRemoved = em.merge(user);
        em.remove(userToBeRemoved);
    }

    public User update(User user) {
        return em.merge(user);
    }

    public List<User> findAll() {
        TypedQuery<User> query = em.createQuery(
                "SELECT u FROM User u ORDER BY u.id", User.class);
        return query.getResultList();
    }
}

a facade:

public class UserFacade {

    private UserDao userDao = new UserDao();

    public void createUser(User user) {
        userDao.save(user);
    }

    public List<User> listAll() {
        List<User> result = userDao.findAll();
        return result;
    }
}

User Managed Bean:

@SessionScoped
@ManagedBean(name = "userMB")
public class UserMB implements Serializable {
    public static final String INJECTION_NAME = "#{userMB}";
    private static final long serialVersionUID = 1L;
    private User user;
    private UserFacade userfacade;


    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void createUser() {
        getUserfacade().createUser(user);
    }

    public UserFacade getUserfacade() {
        if (userfacade == null){
            userfacade = new UserFacade();
        }
        return userfacade;
    }
}

And finally from JSF page I call the method like this:

    <f:view>
        <h:form>
            <h1><h:outputText value="Create/Edit"/></h1>
            <h:panelGrid columns="2">
                <h:outputLabel value="Username:"/>
                <h:inputText id="username" value="#{userMB.user.username}" title="username" />
                <h:outputLabel value="Password:" />
                <h:inputText id="password" value="#{userMB.user.password}" title="password" />
                   <h:outputLabel value="ID:" />
                   <h:inputText id="id" value="#{userMB.user.id}" title="id" />                                      
            </h:panelGrid>
            <h:commandButton action="#{userMB.createUser}" value="create"/>
        </h:form>
    </f:view>

Where is my mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:28:27+00:00Added an answer on June 13, 2026 at 9:28 pm

    @PersistenceContext works only in managed classes. Your UserDAO (and UserFacade) seems to be completely unmanaged and created manually. This way the EntityManager won’t be injected at all. You’d need to manually create it as well. Right now it is null, which thus explains the NullPointerException.

    Make the UserDAO (and UserFacade) a @Stateless EJB

    @Stateless
    public class UserDAO {
        // ...
    }
    

    and use @EJB to inject them (and remove lazy loading in getter)

    @EJB
    private UserFacade userFacade; // Also on userDAO.
    

    Or, if your environment doesn’t support EJBs (e.g. Tomcat), then you’d need to either upgrade it to TomEE or to install OpenEJB on top of it, or to look for an alternate framework to manage your service facades and DAOs. Spring is often used, but why would you choose for it if Java EE 6 already offers the EJB3 awesomeness?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You might think that this question is already answered. However, I couldn't find the
[I hope that this question is not too broad, I think that the subject
First off I would like to say that I think that my question may
This is a general question of sorts, but do you think that it's important
Ok, guys, I think that's the right place to ask a question, because it's
Searching here I found that this question was already asked , but I think
I was reading this post and had a question that I didn't think it
A recent question contains a problem that I many times used to think about
i know that there isn't code inside my question, but i think this is
I've noticed that in trying to get seemingly simple node packages to install with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.