I am receiving an error when ever I try to call any method of the service on the main activity class.
Error log
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String in.ac.iitkgp.alpha.dbService.getUser()' on a null object reference
at in.ac.iitkgp.alpha.MainScreen.onCreateOptionsMenu(MainScreen.java:95)
at android.app.Activity.onCreatePanelMenu(Activity.java:2846)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I have two activities first activity passes the input to the service and starts it using startService(intent); and calls the next activity. Next activity then binds to the service and reads the input data and changes TextView on the UI. I need to pass it through the service as input is needed in many activities.
Service class
public class dbService extends Service {
private final IBinder Mybinder = new dbBinder();
public String user;
public dbService() {
}
@Override
public IBinder onBind(Intent intent) {
return Mybinder;
}
public final String getUser(){
return user;
}
public class dbBinder extends Binder{
dbService getService(){
return dbService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
user = intent.getStringExtra("User");
return START_NOT_STICKY;
}
}
Snippet from first activity
Intent intent = new Intent(this,MainScreen.class);
Intent service = new Intent(this,dbService.class);
String usr = user.getText().toString();
service.putExtra("User",usr);
startService(service);
startActivity(intent);
Snippet from Second activity
dbService Myservice;
boolean bound = false;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
dbBinder binder = (dbBinder) service;
Myservice = binder.getService();
bound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
bound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
.
.
.
Intent i = new Intent(this,dbService.class);
bindService(i,connection, Context.BIND_AUTO_CREATE);
TextView user = (TextView) findViewById(R.id.user);
user.setText(Myservice.getUser());
}
Only accessing the service methods from second activity crashes the app. Everything else seems to work fine. Sorry if it's a dumb question. I am new to java and android app development. Thanks in advance