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 7683671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:50:53+00:00 2026-05-31T18:50:53+00:00

I have a Service class and an main Acitivity class which is supposed to

  • 0

I have a Service class and an main Acitivity class which is supposed to receive broadcasts from the Service class with method sendBroadcast.

It crashes when running the method sendBroadcast from my Service class.

here is part of my Service class (EDITED):

    public static final int STATE_CONNECTING = 1;

    public static final String BT_CONNECTING = "com.android.mypackage.action.BTService.BT_CONNECTING";

    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder() {
            BTService getService() {
                    return BTService.this;
            }
    }

    @Override
    public IBinder onBind(Intent intent) {
            return mBinder;
    }

    public synchronized void setState(int state) {
            mState = state;
            if ( state == STATE_CONNECTING ) {
                    Intent myIntent = new Intent(BT_CONNECTING);
                    try {
                            sendBroadcast(myIntent);
                    }
                    catch (NullPointerException e) {}
            }
    }

And here is part of my Activity class which is supposed to receive the broadcasted intents (EDITED):

    private BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                    if(intent.getAction().equals(BTService.BT_CONNECTING))
                            mState = STATE_CONNECTING;
                    }
            };

    private ServiceConnection mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                    LocalBinder binder = (LocalBinder) service;
                    myService = binder.getService();
                    mBound = true;
            }

            public void onServiceDisconnected(ComponentName arg0) {
                    mBound = false;
            }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
            Intent intent = new Intent(this, BTService.class);
            myService.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

            IntentFilter filter = new IntentFilter(BTService.BTConnecting);
            registerReceiver(receiver, filter);
    }

I get NullPointerException when calling sendBroadcast(intent) method. Any tips on solving this is greatly appreciated.

here is the log:

    FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mypackage/com.android.mypackage.MyActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
    at android.app.ActivityThread.access$2300(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:144)
    at android.app.ActivityThread.main(ActivityThread.java:4937)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:271)
    at com.android.mypackage.BTService.setState(BTService.java:68)
    at com.android.mypackage.BTService.connect(BTService.java:90)
    at com.android.mypackage.MyActivity.onCreate(MyActivity.java:78)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
  • 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-31T18:50:55+00:00Added an answer on May 31, 2026 at 6:50 pm

    From your stack trace, it appears you are somehow directly referencing your BTService service. Since you cut your onCreate() short I can’t be certain how you are doing it, but I will take a guess.

    Did you instantiate this service directly inside of your activity (use new BTService())? If so, then the reason you are getting this error is because your Service has no context bound to it. You must let Android create your service for you by calling startService() or bindService().

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

Sidebar

Related Questions

I have a service class which has overloaded constructors. One constructor has 5 parameters
I have main activity which call other object, for example GPS class. This GPS
I have an AsyncTask which calls my LocationHandler class method getLocation() which runs a
I have a wrapper class (BluetoothDiscoverer) which is instantiated within a Service. This class
I have a web service class that the rest of the framework depends on
I have a property in WCF service class with a initial value, as shown
I have a wcf service. The service itself (class that inherits the ServiceContract )
I currently have service classes that look something like this public class UserService :
Given that I have the following WCF service: class LookUpService { public List<County> GetCounties(string
I have a .NET service, based on ServiceBase : public partial class Service :

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.