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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:15:42+00:00 2026-06-03T03:15:42+00:00

I am trying to find nearby Bluetooth devices so I’m using BroadcastReceiver. Most of

  • 0

I am trying to find nearby Bluetooth devices so I’m using BroadcastReceiver. Most of the time it works fine but sometimes I get this error.

04-30 09:50:15.277: E/AndroidRuntime(847): FATAL EXCEPTION: main
04-30 09:50:15.277: E/AndroidRuntime(847): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND (has extras) } in com.waratah.app.SetMachineActivity$2@40582d20
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.os.Handler.handleCallback(Handler.java:587)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.os.Looper.loop(Looper.java:130)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-30 09:50:15.277: E/AndroidRuntime(847):  at java.lang.reflect.Method.invokeNative(Native Method)
04-30 09:50:15.277: E/AndroidRuntime(847):  at java.lang.reflect.Method.invoke(Method.java:507)
04-30 09:50:15.277: E/AndroidRuntime(847):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-30 09:50:15.277: E/AndroidRuntime(847):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-30 09:50:15.277: E/AndroidRuntime(847):  at dalvik.system.NativeStart.main(Native Method)
04-30 09:50:15.277: E/AndroidRuntime(847): Caused by: java.lang.NullPointerException
04-30 09:50:15.277: E/AndroidRuntime(847):  at com.waratah.app.SetMachineActivity$2.onReceive(SetMachineActivity.java:532)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
04-30 09:50:15.277: E/AndroidRuntime(847):  ... 9 more

It shows NullPointerException but I don’t see how this error can occur because I’m checking the variables to avoid the exception.

// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        String action = intent.getAction();
        int i;
        Machine d;

        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            if (mBtAdapter.isEnabled()) {
                if(D) Log.d(BroadcastReceiver.class.getName(), "Bluetooth is enabled");
                pd.dismiss();                   
            }
            doDiscovery();

        // when bluetooth device is found 
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
            if(D) Log.d(BroadcastReceiver.class.getName(), "found");
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device != null) {

                // Check if the device has name
                if ((device.getName() != null)||(device.getName().length() > 0)) { //LINE 532 HERE 
                    d = new Machine(device);
                    if (d.getPairingState() != BluetoothDevice.BOND_BONDED) {
                        d.setBluetoothState(true);
                        addDevice(d);
                    } else {
                        for (i = 0; i < adapter.getCount(); i++) {
                            if (adapter.getItem(i).getAddress().equals(device.getAddress())) {
                                    adapter.getItem(i).setBluetoothState(true);
                            }
                        }
                    }
                }
            }
        // When discovery is finished, change the Activity title
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            scanButton.setVisibility(View.VISIBLE);

            pb.setVisibility(View.GONE);
            scanning.setVisibility(View.GONE);
        }
    }
};

I have registered all my actions and registered my receiver correctly. This error occurs only occasionally. Is it possible for the onReceive() function to be triggered when there is nothing to receive?

Thanks for your help.

EDIT:
Line 532 is

 if ((device.getName() != null)||(device.getName().length() > 0)) { 

so this means device must be null, but the previous line checks for this.

if (device != null) {

By the way could two broadcast receivers running at the same time cause this kind of error? Does the BroadcastReceiver null the intent after it is received?

  • 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-06-03T03:15:43+00:00Added an answer on June 3, 2026 at 3:15 am

    I suspect you mistakenly used || instead of &&.

    if ((device.getName() != null)||(device.getName().length() > 0)) {

    vs.

    if ((device.getName() != null) && (device.getName().length() > 0)) {

    As originally written, if device.getName() is null, then it attempts to evaluate the second part, which is calling length() on the null value, thus resulting in the NPE you are seeing.

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

Sidebar

Related Questions

Trying to find an example that has css rollover using sprites & sliding door
Trying to dive into Qt big time but haven't done a large project with
Im trying to create a search nearby shops on iphone, but i can not
I am trying to create an app that will help to find nearby places
I tried searching but could not find an answer for this. I am trying
i trying find something like jsfiddle.net resizable layout, but lightweight. i found http://www.jquery-css.com/download-css-jquery-templates http://layout.jquery-dev.net/
Trying to find a standard. The CmdLet will process data - multiple input, defined
Trying to find a way to remove blank pages from a document I wrote
Trying to find out if it's possible to use SQLAlchemy on Heroku. Thanks.
Trying to find the best way of create an overlap/overlay layer to fill the

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.