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 7163691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:56:42+00:00 2026-05-28T13:56:42+00:00

I am using Guice with JDO and Datanucleus in my desktop app. I am

  • 0

I am using Guice with JDO and Datanucleus in my desktop app. I am facing a NPE that I can’t fix, so I hope someone can help me 🙂

I am using properly configured H2 db, with schema created and all my classes are nicely enhanced, so it’s not that. Anyway, I am getting NPE here, at JDORepository class:

public abstract class JdoRepository<T> implements Repository<T>
{
private final Class<T> clazz;
private final Provider<PersistenceManager> pmProvider;

protected JdoRepository(Class<T> clazz, Provider<PersistenceManager> pmProvider)
{
    this.clazz = clazz;
    this.pmProvider = pmProvider;
}
public void persist(T entity)
{
    pmProvider.get().makePersistent(entity);   <---- NPE!
}

My PersistenceManagerFilter looks like that:

    @Singleton
    public class PersistenceManagerFilter implements Filter
    {
    private static final Logger logger =  Logger.getLogger(PersistenceManagerFilter.class.getName());

    private static final ThreadLocal<PersistenceManager> pm = new  ThreadLocal<PersistenceManager>();

    private PersistenceManagerFactory pmf;

    public void init(FilterConfig filterConfig) throws ServletException
    {
        logger.info("Creating PersistenceManagerFactory");
        pmf = JDOHelper.getPersistenceManagerFactory();
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain  chain)
            throws IOException, ServletException
    {
        try
        {
            pm.set(pmf.getPersistenceManager());
            chain.doFilter(request, response);
        }
        finally
        {
            pm.get().close();
        }
    }

    public void destroy()
    {
        logger.info("Closing PersistenceManagerFactory");
        pmf.close();
    }

    /**
     * This module binds the JDO {@link javax.jdo.PersistenceManager} interface to the  provider that allows the
     * implementation to be injected as Provider&lt;PersistenceManager&gt;.
     */
    public static class GuiceModule extends AbstractModule
    {
        @Override
        protected void configure()
        {
            bind(PersistenceManager.class).toProvider(new Provider<PersistenceManager>()
            {
                public PersistenceManager get()
                {
                    return PersistenceManagerFilter.pm.get();
                }
            });
        }
    }
}

And the responsible module:

    public class GuiceModule extends AbstractModule
   {
   @Override
   protected void configure()
   {
       // Enable per-request-thread PersistenceManager injection.
       install(new PersistenceManagerFilter.GuiceModule());
       bind(new TypeLiteral<Repository<Project>>() { }).to(JdoProjectRepository.class);

I am initiating it all that way:

Injector injector = Guice.createInjector(new GuiceModule());
    Main main = injector.getInstance(Main.class);
    main.run();

So repository bindings in main class work fine, as they are redirected to JDORepository. It’s something at lower level, PMF not getting bound properly? Any ideas?

  • 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-05-28T13:56:43+00:00Added an answer on May 28, 2026 at 1:56 pm

    What does your main.run()-method do? Does it call PersistenceManagerFilter#doFilter? If it doesn’t, the value in yourThreadLocal<PersistenceManager> will be null…

    You could override the initialValue() and do something like this:

    ThreadLocal<PersistenceManager> pm = new  ThreadLocal<PersistenceManager>(){
        @Override
        protected PersistenceManager initialValue() {
            return JDOHelper.getPersistenceManagerFactory().getPersistenceManager();
        }
    };
    

    You should also remember to call the ThreadLocal#remove() method in your finally-block.

    Instead of handling the ThreadLocal yourself, you could bind the PersistenceManager directly in the guice-module:

    class GuiceModule extends AbstractModule {
    
        @Provides @RequestScoped
        PersistenceManager providePersistenceManager(){
            return JDOHelper.getPersistenceManagerFactory().getPersistenceManager();
        }
    
        @Override
        protected void configure() {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Google Guice with Jersey (jax-rs). Following method throws an JAXB-Exception (JAXB can't
I'm using Guice to route requests in my web app, and I'd like to
I have some Guice binding code using generics that compiles and functions fine from
I'm using guice for dependency injection with aop from aopalliance . I can't quite
I have a guice based app that now needs multiple instances of a given
I'm currently porting some work from MySQL to Google App Engine/Java. I'm using JDO,
I want to start using guice in an existing application that has legacy code.
plz guide me how I can enable or disable asp.net validation controls using jQuery
I've got a REST app that uses embedded Jetty as the server. Most of
i am using guice with java play framework. i have a simple command interface,

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.