0

My application using Intent Service and Job Intent Service component according to the device supporting Android SDK. Inside my service component, I'm trying to access some of the public defined functions inside my application extended class. While trying to access this functions sometimes this throws NullPointer Exception.

MyApplication.class

public class MyApplication extends MultiDexApplication {

    private static final String TAG = MyApplication.class.getName();

    @Inject
    SharedPreferences mSharedPreferences;

    private BaseApplicationComponent applicationComponent;
    private static MyApplication mInstance;

    {
        mInstance = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        applicationComponent = DaggerBaseApplicationComponent.builder()
                .baseAndroidModule(new BaseAndroidModule(this))
                .remoteDataModule(new RemoteDataModule())
                .localDataModule(new LocalDataModule())
                .build();
        applicationComponent.inject(this);
    }

    public static synchronized MyApplication getInstance() {
        return mInstance;
    }

    public BaseApplicationComponent getComponent(){return 
       applicationComponent;
    }
}

Service Class OnCreate()

@Override
public void onCreate() {
    super.onCreate();
    MyApplication.getInstance().getComponent().plus(new NotificationServiceModule(this))
            .inject(this);
}

Crash Logs :

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleCreateService 
        (ActivityThread.java:3554)
  at android.app.ActivityThread.-wrap4 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage 
      (ActivityThread.java:1786)
  at android.os.Handler.dispatchMessage (Handler.java:105)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6942)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run 
     (Zygote.java:327)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: 
  at com.xyz.app.MyApplicarion.getComponent (MyApplication.java:93)
  at com.xyz.app.pushnotification.NotificationJobService.onCreate 
      (NotificationJobService.java:83)
  at android.app.ActivityThread.handleCreateService 
      (ActivityThread.java:3544)
  at android.app.ActivityThread.-wrap4 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage 
      (ActivityThread.java:1786)
  at android.os.Handler.dispatchMessage (Handler.java:105)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6942)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run 
      (Zygote.java:327)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)

Is there any problem in my application or accessing code or give me any suggestions to solve this problem.

baldraider
  • 1,049
  • 2
  • 18
  • 48

0 Answers0