I am trying to make my activity communicate with a running background service, but keeps getting a NullPointerException, which doesn't make any sense.. for me at least. Hope someone else can figure out what the problem might be.
First here is my LogCat output:
03-17 20:16:56.390: E/AndroidRuntime(11563): FATAL EXCEPTION: main
03-17 20:16:56.390: E/AndroidRuntime(11563): java.lang.NullPointerException
03-17 20:16:56.390: E/AndroidRuntime(11563): at com.takenumber.TakeNumber.sendMessageToService(TakeNumber.java:240)
03-17 20:16:56.390: E/AndroidRuntime(11563): at com.takenumber.TakeNumber$2.onClick(TakeNumber.java:334)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.view.View.performClick(View.java:4204)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.view.View$PerformClick.run(View.java:17355)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.os.Handler.handleCallback(Handler.java:725)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.os.Looper.loop(Looper.java:137)
03-17 20:16:56.390: E/AndroidRuntime(11563): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-17 20:16:56.390: E/AndroidRuntime(11563): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 20:16:56.390: E/AndroidRuntime(11563): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 20:16:56.390: E/AndroidRuntime(11563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
03-17 20:16:56.390: E/AndroidRuntime(11563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-17 20:16:56.390: E/AndroidRuntime(11563): at dalvik.system.NativeStart.main(Native Method)
What I do in my activity:
is to start the service and bind it:
serviceIntent = new Intent(context, ServerService.class);
serviceIntent.putExtra("messageType", StaticValues.MESSAGE_START_SERVICE);
serviceIntent.putExtra("devicetype", StaticValues.DEVICE_TAKENUMBER);
serviceIntent.putExtra("MESSENGER", new Messenger(messageHandler));
startService(serviceIntent);
bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
and create a serviceConnection:
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (isServer) {
boundServiceServer = ((ServerService.CustomBinder) service).getService();
} else {
boundServiceClient = ((ClientService.CustomBinder) service).getService();
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
boundServiceClient = null;
boundServiceServer = null;
}
};
And then I have a click listener for a button to call a method in my service:
categoryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button buttonClicked = (Button)v;
int categoryID = v.getId();
String categoryTitle = buttonClicked.getText().toString();
// TODO: Change to send only to showline devices
boundServiceServer.getMessagesFromUI(StaticValues.MESSAGE_NUMBER_TAKEN + StaticValues.MESSAGE_DELIMITER + categoryID + StaticValues.MESSAGE_DELIMITER + categoryTitle);
}
});
In my service I do this:
Create a "binder
":
public class CustomBinder extends Binder {
public ServerService getService() {
return ServerService.this;
}
}
The method getMessagesFromUI()
in my service (to receive strings from activity) simply looks like this:
public void getMessagesFromUI(String message) {
// DO stuff
}
Of course onBind()
method os overridden:
@Override
public IBinder onBind(Intent arg0) {
return binder;
}
I really hope someone can figure out the problem as I am completely lost right now. If you need more informatione, just ask.
Edit: Line 240 (NullPointerException)
is this one:
boundServiceServer.getMessagesFromUI(messageString);
Additional info:
I've pasted some of my code on pastebin on the following adresses.
The TakeNumber activity is here: http://pastebin.com/DxrWfEYM
And the ServerService is here: http://pastebin.com/7QjiQbTi
I have also inserted "syso"'s in the code to find out what's going on. THis is the logcat output after this:
03-18 14:23:05.740: D/dalvikvm(20260): GC_CONCURRENT freed 213K, 16% free 4276K/5064K, paused 2ms+3ms, total 24ms
03-18 14:23:05.790: I/System.out(20260): server connecting
03-18 14:23:05.810: I/System.out(20260): onBind
03-18 14:23:05.830: D/libEGL(20260): loaded /vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
03-18 14:23:05.840: D/libEGL(20260): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
03-18 14:23:05.840: D/libEGL(20260): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so
03-18 14:23:05.890: D/OpenGLRenderer(20260): Enabling debug mode 0
03-18 14:23:05.910: I/System.out(20260): server binding
03-18 14:23:05.910: I/System.out(20260): CustomBinder
03-18 14:23:13.710: I/System.out(20260): trying to send to server
03-18 14:23:13.720: D/AndroidRuntime(20260): Shutting down VM
03-18 14:23:13.720: W/dalvikvm(20260): threadid=1: thread exiting with uncaught exception (group=0x41954930)
03-18 14:23:13.720: E/AndroidRuntime(20260): FATAL EXCEPTION: main
03-18 14:23:13.720: E/AndroidRuntime(20260): java.lang.NullPointerException
03-18 14:23:13.720: E/AndroidRuntime(20260): at com.takenumber.TakeNumber.sendMessageToService(TakeNumber.java:244)
03-18 14:23:13.720: E/AndroidRuntime(20260): at com.takenumber.TakeNumber$2.onClick(TakeNumber.java:339)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.view.View.performClick(View.java:4204)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.view.View$PerformClick.run(View.java:17355)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.os.Handler.handleCallback(Handler.java:725)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.os.Handler.dispatchMessage(Handler.java:92)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.os.Looper.loop(Looper.java:137)
03-18 14:23:13.720: E/AndroidRuntime(20260): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-18 14:23:13.720: E/AndroidRuntime(20260): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 14:23:13.720: E/AndroidRuntime(20260): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 14:23:13.720: E/AndroidRuntime(20260): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
03-18 14:23:13.720: E/AndroidRuntime(20260): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-18 14:23:13.720: E/AndroidRuntime(20260): at dalvik.system.NativeStart.main(Native Method)
The app crashes after the button is called, when I try to send data to the service.
I came to a solution myself. I have posted my activity and service code on pastebin.
The Activity: http://pastebin.com/jMgraK2r
The ServerService: http://pastebin.com/yquZAi2i
For some reason, the messenger object from the activity to the service was setting itself to null somehow. By declaring it as static, the problem was solved.
I hope anyone can learn anything from this. Thanks to all the responses above.