0

I'm doing android app where I need to communicate with special tablo via Bluetooth.

I have an error with java.lang.NullPointerException

I’ve announced everything that I need. What I need to do?

Java class

public class SearchTabloActivity extends AppCompatActivity {

    ListView listView;

    BluetoothAdapter mBluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_tablo);

        listView = (ListView)findViewById(R.id.listView);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if (mBluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(), "Bluetooth not support!", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(SearchTabloActivity.this, MainActivity.class));
        }

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

        List<String> s = new ArrayList<String>();
        for(BluetoothDevice bt : pairedDevices)
            s.add(bt.getName() + "\n" + bt.getAddress());

        listView.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_search_tablo, s));

    }
}

Error

2020-04-24 09:47:56.208 12121-12121/com.kvaksmanyt.exoy E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.kvaksmanyt.exoy, PID: 12121
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kvaksmanyt.exoy/com.kvaksmanyt.exoy.tablo.SearchTabloActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()' on a null object reference
Dmitry
  • 126
  • 7

1 Answers1

1

Use return to stop code in any void method

KvaksMan play
  • 230
  • 2
  • 10