Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:23:40+00:00 2026-05-31T22:23:40+00:00

I have created a service class for my network connection so that my app

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:23:42+00:00Added an answer on May 31, 2026 at 10:23 pm

    This is almost certainly a race condition. You start a thread in onCreate(Bundle), and that thread is expecting mService to refer to an instance of some sort, but that instance isn’t populated until your Service is bound (which happens in onStart()).

    I am not sure, but can your Service be bound in onCreate(Bundle) (specifically, before you start your Thread), or can your thread be started in onStart() (specifically, after your Service is 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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already created a windows service that monitors the network for some specific
I have created a web service via WCF. Then I exposed it as a
We have created a WCF service hosted in a windows service that handles Authentication
I have a WCF service. I can return a concrete class without a problem,
I have written a network server class that maintains a std::set of network clients.
I have created a Windows Service that will be calling out to some COM
I have created a C# Windows Service application that starts a FileSystemWatcher to monitor
I have a issue where I'm trying to create a class that uses the
I have created an web service I need to publish this service. I need
I have created a web service which has a couple of methods developed using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.