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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:19:03+00:00 2026-06-15T19:19:03+00:00

I’m getting this NullPointerException for no reason. I am trying to send accelerometer ‘x’

  • 0

I’m getting this NullPointerException for no reason. I am trying to send accelerometer ‘x’ value to MCU which is connected to my phone by bluetooth. Everything seems to be fine until it starts to send bytes.

MainActivity.class:

    private void getAccelerometer(SensorEvent event) {

        float[] values = event.values;
        // Movement
        float x = values[0];
        float y = values[1];
        float z = values[2]; 

        // Convert float value to integer for progress bars because 
        // they dont't support float value
        int mProgressStatus_x = (int)x;
        int mProgressStatus_y = (int)y;
        int mProgressStatus_z = (int)z;

        // Set converted x, y and z values to progress bars
        // Add to each progress bar value 10 because progress bar dosen't support negative value
        mProgressBar_x.setProgress(mProgressStatus_x + 10); 
        mProgressBar_y.setProgress(mProgressStatus_y + 10);
        mProgressBar_z.setProgress(mProgressStatus_z + 10);
        mX = Float.toString(x);
        SendBytes(mX); 
}
    private void SendBytes (String mX) {

        if (mConnectionStatus == 1 && mX != null) {

            // mX is the accelerometer value which is converted to the String
            byte[] out = mX.getBytes();
            // mBluetoothService is BluetoothService.java   
            mBluetoothService.write(out); // Line 150
                }

    }

BluetoothService.class:

     public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {

         // Cancel the thread that completed the connection
         if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null; }
         // Cancel the ConnectedThread to make sure that it's not running currently connection
         if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
         // Start the ConnectedThread to manage connection and perform transmission 
         mConnectedThread = new ConnectedThread(socket);
         mConnectedThread.start();
         // Send the name of the connected device back to the UI Activity
         Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_DEVICE_NAME);
         Bundle bundle = new Bundle();
         bundle.putString(MainActivity.DEVICE_NAME, device.getName());
         msg.setData(bundle);
         mHandler.sendMessage(msg);
         // Set state of connection to STATE_CONNECTED
         setState(STATE_CONNECTED);
         mmConnectionStatus = 1;
         System.out.println("State connected");
     }

public void write(byte[] out) {
     ConnectedThread r;
     // Synchronize a copy of the ConnectedThread
     synchronized (this) {

         r = mConnectedThread;
     }
     if(out != null){   
     r.write(out); } // Line 133
    }

private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

    // Thread for managing connection 
    public ConnectedThread(BluetoothSocket socket) {
        System.out.println("ConnectedThread started");
        mmSocket = socket;  
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try { // Get the input and output streams, using temp objects because
              // member streams are final
              tmpIn = socket.getInputStream();
              tmpOut = socket.getOutputStream(); 
              System.out.println("creating socket");
              }
        catch (IOException e) {
            System.out.println("tmp socket not created");
        }
    mmInStream = tmpIn;
    mmOutStream = tmpOut;
    }


    public void run() {
        System.out.println("ConnectedThread status" + mConnectedThread);


        // Buffer store for the stream
        byte[] buffer = new byte[1024];
        // Bytes returned from the read()
        int bytesIn;
        // Keep listening until an  occurs
        while(true) {
            try {
                bytesIn = mmInStream.read(buffer);
            } catch (IOException e) {
                break;
            }

        }
    }
    // Call this from the MainActivity to send data to the remote device
    public void write(byte[] bytes) {
        System.out.println(bytes);
            if(mState == STATE_CONNECTED && bytes != null){
            try {
                mmOutStream.write(bytes);
                mmOutStream.flush();
            } catch (IOException e) {
                System.out.println("Exception during write" + e);
            }
            }
}
    // Call this from the MainActivity to shutdown the connection
    public void cancel() {

        try {
            mmSocket.close();

        } catch (IOException e) {}

    }
}

Log:

12-12 03:22:59.228: D/libEGL(12554): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
12-12 03:22:59.228: D/libEGL(12554): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
12-12 03:22:59.228: D/libEGL(12554): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
12-12 03:22:59.368: D/OpenGLRenderer(12554): Enabling debug mode 0
12-12 03:23:01.767: D/dalvikvm(12554): GC_CONCURRENT freed 111K, 2% free 9181K/9324K, paused 4ms+7ms, total 33ms
12-12 03:23:01.892: D/BluetoothAdapter(12554): enable(): BT is already enabled..!
12-12 03:23:08.509: I/System.out(12554): ConnectThread statusThread[Thread-919,5,main]
12-12 03:23:08.509: W/BluetoothAdapter(12554): getBluetoothService() called with no BluetoothManagerCallback
12-12 03:23:08.517: D/BluetoothSocket(12554): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[46]}
12-12 03:23:09.017: E/ActivityThread(12554): Activity com.example.bluetoothc.settings has leaked IntentReceiver com.example.bluetoothc.settings$2@4151ecb8 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-12 03:23:09.017: E/ActivityThread(12554): android.app.IntentReceiverLeaked: Activity com.example.bluetoothc.settings has leaked IntentReceiver com.example.bluetoothc.settings$2@4151ecb8 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
12-12 03:23:09.017: E/ActivityThread(12554):    at com.example.bluetoothc.settings.onCreate(settings.java:106)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.Activity.performCreate(Activity.java:5104)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.os.Looper.loop(Looper.java:137)
12-12 03:23:09.017: E/ActivityThread(12554):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-12 03:23:09.017: E/ActivityThread(12554):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 03:23:09.017: E/ActivityThread(12554):    at java.lang.reflect.Method.invoke(Method.java:511)
12-12 03:23:09.017: E/ActivityThread(12554):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-12 03:23:09.017: E/ActivityThread(12554):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-12 03:23:09.017: E/ActivityThread(12554):    at dalvik.system.NativeStart.main(Native Method)
12-12 03:23:09.814: I/System.out(12554): making connection to remote device
12-12 03:23:09.814: I/System.out(12554): ConnectedThread started
12-12 03:23:09.814: I/System.out(12554): creating socket
12-12 03:23:09.822: I/System.out(12554): ConnectedThread statusThread[Thread-920,5,main]
12-12 03:23:09.822: I/System.out(12554): State connected
12-12 03:23:09.837: I/System.out(12554): 1
12-12 03:23:09.876: D/AndroidRuntime(12554): Shutting down VM
12-12 03:23:09.876: W/dalvikvm(12554): threadid=1: thread exiting with uncaught exception (group=0x40c25930)
12-12 03:23:09.876: E/AndroidRuntime(12554): FATAL EXCEPTION: main
12-12 03:23:09.876: E/AndroidRuntime(12554): java.lang.NullPointerException
12-12 03:23:09.876: E/AndroidRuntime(12554):    at com.example.bluetoothc.BluetoothService.write(BluetoothService.java:133)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at com.example.bluetoothc.MainActivity.SendBytes(MainActivity.java:150)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at com.example.bluetoothc.MainActivity.getAccelerometer(MainActivity.java:140)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at com.example.bluetoothc.MainActivity.onSensorChanged(MainActivity.java:111)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at android.hardware.SystemSensorManager$ListenerDelegate$1.handleMessage(SystemSensorManager.java:204)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at android.os.Looper.loop(Looper.java:137)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at java.lang.reflect.Method.invoke(Method.java:511)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-12 03:23:09.876: E/AndroidRuntime(12554):    at dalvik.system.NativeStart.main(Native Method)
12-12 03:23:13.884: I/Process(12554): Sending signal. PID: 12554 SIG: 9
  • 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-15T19:19:04+00:00Added an answer on June 15, 2026 at 7:19 pm

    There’s a very clear reason.

    Activity com.example.bluetoothc.settings has leaked IntentReceiver com.example.bluetoothc.settings$2@4151ecb8 that was originally registered here. Are you missing a call to unregisterReceiver()?
    

    You are leaking an IntentReceiver. When an event is routed to the Receiver (because you didn’t unregister it) most likely after the activity is paused/destroyed, it throws a NullPointerException somewhere because it has lost all the context references, or they are not valid anymore.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

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.