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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:22:11+00:00 2026-06-05T19:22:11+00:00

So my code has been working fine, and I was added some extra functionality

  • 0

So my code has been working fine, and I was added some extra functionality to the GUI, and now all of the sudden I’m getting a null pointer exception. I can’t figure out what I could have changed to make this happen. I have dumbed down the code to where the error is.

public class AndroidBluetooth extends Activity {

/** Called when the activity is first created. */
private static BluetoothAdapter myBtAdapter;
private static BluetoothDevice myBtDevice;
private ArrayAdapter<String> btArrayAdapter;
private ArrayList<BluetoothDevice> btDevicesFound = new ArrayList<BluetoothDevice>();
private Button btnScanDevice;
private TextView stateBluetooth;
private ListView listDevicesFound;
private InputStream iStream;
private OutputStream oStream;
private BluetoothSocket btSocket;
private String newDeviceAddress;
private BroadcastReceiver mReceiver;

// Intent request codes
private static final int REQUEST_CONNECT_DEVICE = 1;

private static TextView mTitle;

// Name of the connected device
private String mConnectedDeviceName = null;

/**
 * Set to true to add debugging code and logging.
 */
public static final boolean DEBUG = true;

/**
 * Set to true to log each character received from the remote process to the
 * android log, which makes it easier to debug some kinds of problems with
 * emulating escape sequences and control codes.
 */
public static final boolean LOG_CHARACTERS_FLAG = DEBUG && false;

/**
 * Set to true to log unknown escape sequences.
 */
public static final boolean LOG_UNKNOWN_ESCAPE_SEQUENCES = DEBUG && false;

private static final int REQUEST_ENABLE_BT = 2;


// Member fields
//private final BluetoothAdapter mAdapter;
//private final Handler mHandler;
//private ConnectThread mConnectThread;
//private ConnectedThread mConnectedThread;
//private int mState;

//private EmulatorView mEmulatorView;

// Constants that indicate the current connection state
public static final int STATE_NONE = 0;       // we're doing nothing
public static final int STATE_LISTEN = 1;     // now listening for incoming connections
public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
public static final int STATE_CONNECTED = 3;  // now connected to a remote device

//public boolean customTitleSupported;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    //customTitleSupported = requestWindowFeature( Window.FEATURE_CUSTOM_TITLE );
    // Set up window View
    setContentView(R.layout.main);

    stateBluetooth = (TextView) findViewById ( R.id.titleTvRight );
    startBluetooth();
    myBtAdapter = null;
    CheckBlueToothState();

    //customTitleBar( getText( R.string.app_name).toString(), getText( R.string.app_name).toString() );
}
/**
public void customTitleBar( String left, String right ) {
    if( right.length() > 20 ) right = right.substring( 0, 20 );

    if( customTitleSupported ) {
        getWindow().setFeatureInt( Window.FEATURE_CUSTOM_TITLE, R.layout.customlayoutbar );
        TextView titleTvLeft = (TextView) findViewById( R.id.titleTvLeft );
        TextView titleTvRight = (TextView) findViewById( R.id.titleTvRight );

        titleTvLeft.setText( left );
        titleTvRight.setText( right );

    }
}
*/
public boolean onCreateOptionsMenu( Menu menu ) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate( R.menu.option_menu, menu );
    return true;
}

public boolean onOptionsItemSelected( MenuItem item ) {
    switch( item.getItemId() ) {
    case R.id.connect:
        startActivityForResult( new Intent( this, DeviceList.class ), REQUEST_CONNECT_DEVICE  );
        return true;
    case R.id.preferences:
        return true;
    default:
        return super.onContextItemSelected( item );
    }
}

private void CheckBlueToothState() {
    Log.i("HUH", "0");
    if( myBtAdapter == null ) {
        Log.i("HUH", "1");
        stateBluetooth.setText("Bluetooth NOT supported" );
    } else {
        Log.i("HUH","2");
        if( myBtAdapter.isEnabled() ) {
            Log.i("HUH","3");
            if( myBtAdapter.isDiscovering() ) {
                Log.i("HUH","4");
                stateBluetooth.setText( "Bluetooth is currently " +
                        "in device discovery process." );
            } else {
                Log.i("HUH","5");
                stateBluetooth.setText( "Bluetooth is Enabled." );
                btnScanDevice.setEnabled( true );
            }
        } else {
            Log.i("HUH","6");
            stateBluetooth.setText( "Bluetooth is NOT enabled" );
            Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE );
            startActivityForResult( enableBtIntent, REQUEST_ENABLE_BT );
        }
    }
}

private Button.OnClickListener btnScanDeviceOnClickListener = new Button.OnClickListener() {
    public void onClick( View arg0 ) {

    }
};


@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
    if( requestCode == REQUEST_ENABLE_BT ) {
        CheckBlueToothState();
    }
}

//In SDK15 (4.0.3) this method is now public as
//Bluetooth.fetchUuisWithSdp() and BluetoothDevice.getUuids()
public ParcelUuid[] servicesFromDevice(BluetoothDevice device) {
    try {
        Class cl = Class.forName("android.bluetooth.BluetoothDevice");
        Class[] par = {};
        Method method = cl.getMethod("getUuids", par);
        Object[] args = {};
        ParcelUuid[] retval = (ParcelUuid[]) method.invoke(device, args);
        return retval;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
    public void onReceive( Context context, Intent intent ) {
        String action = intent.getAction();
        if( BluetoothDevice.ACTION_FOUND.equals( action ) ) {
            BluetoothDevice btDevice = intent.getParcelableExtra( BluetoothDevice.EXTRA_DEVICE );
            btDevicesFound.add( btDevice );
            btArrayAdapter.add( btDevice.getName() + "\n" + btDevice.getAddress() );
            btArrayAdapter.notifyDataSetChanged();
        }           
    }
};
public static void startBluetooth(){
    try {
        myBtAdapter = BluetoothAdapter.getDefaultAdapter();
        myBtAdapter.enable();
    } catch ( NullPointerException ex ) {
        Log.e( "Bluetooth", "Device not available" );
    }
}

public static void stopBluetooth() {
    myBtAdapter.disable();
}

}

Here is the LogCat:

06-13 16:34:33.654: I/HUH(17008): 0
06-13 16:34:33.654: I/HUH(17008): 1
06-13 16:34:33.662: D/AndroidRuntime(17008): Shutting down VM
06-13 16:34:33.662: W/dalvikvm(17008): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-13 16:34:33.662: E/AndroidRuntime(17008): FATAL EXCEPTION: main
06-13 16:34:33.662: E/AndroidRuntime(17008): java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.exercise.AndroidBluetooth/com.exercise.AndroidBluetooth.AndroidBluetooth}: java.lang.NullPointerException
06-13 16:34:33.662: E/AndroidRuntime(17008):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at android.os.Looper.loop(Looper.java:130)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at android.app.ActivityThread.main(ActivityThread.java:3683)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at java.lang.reflect.Method.invokeNative(Native Method)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at java.lang.reflect.Method.invoke(Method.java:507)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at dalvik.system.NativeStart.main(Native Method)
06-13 16:34:33.662: E/AndroidRuntime(17008): Caused by: java.lang.NullPointerException
06-13 16:34:33.662: E/AndroidRuntime(17008):    at com.exercise.AndroidBluetooth.AndroidBluetooth.CheckBlueToothState(AndroidBluetooth.java:165)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at com.exercise.AndroidBluetooth.AndroidBluetooth.onCreate(AndroidBluetooth.java:124)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-13 16:34:33.662: E/AndroidRuntime(17008):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-13 16:34:33.662: E/AndroidRuntime(17008):    ... 11 more
  • 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-05T19:22:14+00:00Added an answer on June 5, 2026 at 7:22 pm

    I wish you had posted the whole class… we don’t know if you have cut out the cause or not… for example, a possible reason;

    Is stateBluetooth declared as a class variable or a local variable? If you slipped.and created it int the onCreate, that could be why you get an NPE in the other method.

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

Sidebar

Related Questions

Code has been amended and now it works as follows when you click on
I am getting LNK2001 error. The code has been included below. Can someone please
Below I present you some code which has completely been butchered by me. $(.gig).hover(function()
This has been bugging me for some time now. My problem is the following.
I have noticed a problem with Twitterizer in that it's has been working fine
My ability model for cancan has been working well for 6+ months now. One
I've developed a simple website using facebook connect, and it has been working fine
I have a method myMethod(int a, String b) that has been working fine. However,
This process has been working fine in Chrome, but in Firefox 7.0.1 there are
I have a list property tag_list = db.StringListProperty() This has been working fine 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.