I have created a service class for my network connection so that my app can create a connection and then access the input and output streams throughout the program. I have instantiated the service class in the first activity but whenever I try and access a get method from the service class i get a null pointer exception. The weird thing is, when i debug the program it does not get this error.
Here is the class that is getting the code:
public class WaitingActivity extends Activity implements Runnable{
WaitingNetwork roleGetter;
String role;
int gotrole = 0;
Intent intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waitingroom);
Thread thread = new Thread(this);
thread.start();
ProgressDialog dialog = ProgressDialog.show(WaitingActivity.this, "",
"Loading. Please wait...", true);
}
Networking mService;
boolean mBound = false;
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, Networking.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
/** Called when a button is clicked (the button in the layout file attaches to
* this method with the android:onClick attribute) */
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
ObjectInputStream stream;
public void getStream(){
stream = mService.getStateOis();
}
@Override
public void run() {
getStream();
roleGetter = new WaitingNetwork(stream);
Thread fred = new Thread(roleGetter);
fred.start();
Here’s the stack trace:
03-26 15:41:16.374: E/AndroidRuntime(1301): FATAL EXCEPTION: Thread-11
03-26 15:41:16.374: E/AndroidRuntime(1301): java.lang.NullPointerException
03-26 15:41:16.374: E/AndroidRuntime(1301): at com.DrawTastic.WaitingActivity.getStream(WaitingActivity.java:82)
03-26 15:41:16.374: E/AndroidRuntime(1301): at com.DrawTastic.WaitingActivity.run(WaitingActivity.java:88)
03-26 15:41:16.374: E/AndroidRuntime(1301): at java.lang.Thread.run(Thread.java:1019)
03-26 15:41:18.454: E/WindowManager(1301): Activity com.DrawTastic.WaitingActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405320a0 that was originally added here
03-26 15:41:18.454: E/WindowManager(1301): android.view.WindowLeaked: Activity com.DrawTastic.WaitingActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405320a0 that was originally added here
03-26 15:41:18.454: E/WindowManager(1301): at android.view.ViewRoot.<init>(ViewRoot.java:258)
03-26 15:41:18.454: E/WindowManager(1301): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-26 15:41:18.454: E/WindowManager(1301): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-26 15:41:18.454: E/WindowManager(1301): at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.Dialog.show(Dialog.java:241)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ProgressDialog.show(ProgressDialog.java:107)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ProgressDialog.show(ProgressDialog.java:90)
03-26 15:41:18.454: E/WindowManager(1301): at com.DrawTastic.WaitingActivity.onCreate(WaitingActivity.java:33)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-26 15:41:18.454: E/WindowManager(1301): at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 15:41:18.454: E/WindowManager(1301): at android.os.Looper.loop(Looper.java:123)
03-26 15:41:18.454: E/WindowManager(1301): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-26 15:41:18.454: E/WindowManager(1301): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 15:41:18.454: E/WindowManager(1301): at java.lang.reflect.Method.invoke(Method.java:507)
03-26 15:41:18.454: E/WindowManager(1301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-26 15:41:18.454: E/WindowManager(1301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-26 15:41:18.454: E/WindowManager(1301): at dalvik.system.NativeStart.main(Native Method)
This is almost certainly a race condition. You start a thread in
onCreate(Bundle), and that thread is expectingmServiceto refer to an instance of some sort, but that instance isn’t populated until yourServiceis bound (which happens inonStart()).I am not sure, but can your Service be bound in
onCreate(Bundle)(specifically, before you start yourThread), or can your thread be started inonStart()(specifically, after yourServiceis bound)? Doing either one of those can help, but your best bet would be to use some sort of listener pattern, which utilizes callbacks (much as the android application framework does).