Dependency injection with Play Framework and Guice leads to NullPointerException on Application start

okarahan

I have following code:

import javax.inject.Inject;
import models.Subject;
import models.dao.SchoolDao;
import models.dao.SubjectDao;
import play.Application;
import play.GlobalSettings;
import play.db.jpa.JPA;
import play.db.jpa.Transactional;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;


public class Global extends GlobalSettings{

private Injector injector;

@Inject
private SubjectDao subjectDao;

@Override
public void onStart(Application application) {
    injector = Guice.createInjector(new AbstractModule() {
        @Override
        protected void configure() {
            bind(SchoolDao.class);
            bind(SubjectDao.class);
        }
    });

    insertInitialData();
}

@Override
public <A> A getControllerInstance(Class<A> aClass) throws Exception {
    return injector.getInstance(aClass);
}

@Transactional
private void insertInitialData(){

    if(subjectDao.countAll() == 0){
        Subject deutsch = new Subject();
        deutsch.setName("Deutsch");
        subjectDao.create(deutsch); 

        Subject englisch = new Subject();
        deutsch.setName("Englisch");
        subjectDao.create(englisch);

        Subject mathe = new Subject();
        deutsch.setName("Mathematik");
        subjectDao.create(mathe);

        Subject hab = new Subject();
        deutsch.setName("Hausaufgabenbetreuung");
        subjectDao.create(hab);
    }
}

}

As you see I want to seed my database with initial values on startup. I have a data access object "subjectDao" which should be injected. This works on other positions of my application but due to some reason not on applicatin startup. I get a NullPointerException and my subjectDao is null. Has someone an idea.

Esko

Looks like you are not actually injecting anything to Global, you're just binding your classes (and handling controller injection, but that's irrelevant).

You should do that by calling eg. Injector.injectMembers(this) to get the subjectDao of your Global properly injected.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Scala Play Guice Dependency Injection Fails

Play framework dependency injection is not working

Scala Dependency Injection in Play Framework

Play Framework without dependency injection?

Play Framework PathBindable with Dependency Injection

Play Framework: Dependency Injection in Sub-Templates

Scala play Guice injection

Dependency Injection leads to proliferation of factories?

Dependency Injection with Guice and "Law of Demeter"

Google Guice runtime dependency injection

Play Framework 2.5 dependency injection in form object bean (Java)

Dependency injection with abstract class and object in Play Framework 2.5

ScalaWS in play framework 2.5 and dependency injection of WSClient in custom class

Play framework compile-time dependency injection and singleton

Play Framework 2.4 - Dependency injection to replace GlobalSettings.onStart()

Play framework java dependency injection - when to use singletons

Play Framework dependency injection Object vs @Singleton Class

Scala PLAY Guice Injection and Implicits

How to access Play Framework 2.4 guice Injector in application?

Is it possible to implement dependency injection without using service locator at the start of an application?

Python Dependency Injection Framework

Entity Framework Dependency Injection

Dependency injection and Entity Framework

Dependency Injection framework

When and where to use dependency injection with Guice?

Google Guice vs. PicoContainer for Dependency Injection

Dependency injection: Scoping by region (Guice, Spring, Whatever)

Guice assisted injection deeper down the dependency hierarchy

Null Pointer Exception - Dependency Injection - Google Guice