Breaking News

android – Creating Singleton Classes with Dagger2 throws NullPointerException-Exceptionshub

Questions:

I am trying to create Singleton Objects for a few classes using Dagger. my code structure is below. I get NullPointer on calling any methods in all dependencies injected using dagger.
I have used here and here {this does not create DaggerApplicationGraph for me}

Main build.gradle dependencies

...
api 'com.google.dagger:dagger-android:2.24'
implementation "com.google.dagger:dagger:2.24"
annotationProcessor "com.google.dagger:dagger-compiler:2.13"
compileOnly 'javax.annotation:jsr250-api:1.0'
implementation 'javax.inject:javax.inject:1'

Module build.gradle dependencies

...
implementation "com.google.dagger:dagger:2.24"
annotationProcessor "com.google.dagger:dagger-compiler:2.13"
compileOnly 'javax.annotation:jsr250-api:1.0'
implementation 'javax.inject:javax.inject:1'

FactoryUtils.java

@Module
public class FactoryUtils {

    @Provides
    @Singleton
    FirestoreChatEngine getFireStoreEngineFactory(){
        return new FirestoreChatEngine();
    }

    @Provides
    @Singleton
    FirestoreComposer getFireStoreDocumentFactory(){
        return new FirestoreComposer();
    }

    @Provides
    @Singleton
    CentralDataManager getDataAccessorFactory(){return new CentralDataManager();
}

MessageGateway.java

public class MessageGateway {
        @Inject
        FirestoreChatEngine engine;

        private MessageGateway(){
            //ApplicationGraph applicationGraph = DaggerApplicationGraph.create();
            //engine = applicationGraph.createFireStoreEngine();
        }

        public static MessageGateway getInstance() throws GatewayInitException{
            if(null == instance){
                throw new GatewayInitException(GatewayInitException.MESSAGE_NOT_CREATED);
            }
            return instance;
        }
        public void foo(){
            engine.otherMethods();
        }
}

FirestoreChatEngine.java

@Singleton
public class FirestoreChatEngine {

    private FirebaseFirestore database;

    @Inject
    FirestoreComposer composer;
    @Inject
    CentralDataManager accessor;

    @Inject
    public FirestoreChatEngine(){
        this.database = FirebaseFirestore.getInstance();
    }
    public void otherMethods(){
        composer.otherMethods();
        accessor.foo();
    }
}

FirestoreComposer.java

@Singleton
public class FirestoreComposer {

    private FirebaseFirestore database;

    @Inject
    public FirestoreComposer() {
        this.database = FirebaseFirestore.getInstance();
    }
   public void otherMethods(){}
}

CentralDataStore.java

@Singleton
public class CentralDataManager {
    @Inject
    public CentralDataManager(){
        init();
    }
    public void foo(){
       // do something;
    }
}

How to&Answers:

About rankhawks

Check Also

c# – Acumatica upgrade from 18.210 to 18.217 (Unable to cast object of type ‘PX.Data.PXResult`2[PX.Objects.FS.FSServiceOrder,PX.Objects.AR.Customer]’-Exceptionshub

Questions: The source code of the button action is public PXAction CreateSalesOrder; [PXUIField(DisplayName = "Create …

jQuery offline slideshow on Android-Exceptionshub

(function($){ $.fn.chocolate = function(args) { // Default Options var options = $.extend({ interval : 1000, …

c# – How to handle mocking (Rhino Mock) and unity container in different unit test classes?-Exceptionshub

Questions: I am working on a large desktop application. The application communicates with number of …

javascript – Actualize Database Data with Functions Cloud-Exceptionshub

Questions: I am developing an Android application and I have a problem that I cannot …

valgrind for Android devices (Crystax). configure: error: C compiler cannot create executables-Exceptionshub

Questions: Using and adapting the script(https://gist.github.com/smits/27060b310546ca6f76568f24838118c3) I get and error ” C compiler cannot create …

Leave a Reply

Your email address will not be published. Required fields are marked *