0

When the function send is called i get the followwing error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream android.bluetooth.BluetoothSocket.getOutputStream()' on a null object reference

Why does it work in my Oncreate but not in send?

public class MainActivity extends AppCompatActivity {
    private Button sende;
    public BluetoothSocket bluetoothSocket;
    BluetoothAdapter bluetoothAdapter;

    static final UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Bluetooth_an();
        sende = findViewById(R.id.button);
        BluetoothDevice blue = bluetoothAdapter.getRemoteDevice("00:19:08:36:1B:BA");
        System.out.println(blue.getName());
        System.out.println(blue.getUuids().toString());
        BluetoothSocket bluetoothSocket = null;
        int counter = 0;
        do {
            try {
                bluetoothSocket = blue.createRfcommSocketToServiceRecord(mUUID);
                System.out.println(bluetoothSocket);
                bluetoothSocket.connect();
                System.out.println(bluetoothSocket.isConnected());
            } catch (IOException e) {
                e.printStackTrace();
            }
            counter++;
        } while (!bluetoothSocket.isConnected() && counter < 20);

        sende.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send();
            }
        });

        try {
            OutputStream outputStream = bluetoothSocket.getOutputStream();
            outputStream.write(1);
        } catch (IOException e) {
            e.printStackTrace();
        }

Until here it is working

    }
    private void Bluetooth_an() {
        ...
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
       ...
            }}}
    private void send(){
        try {
            OutputStream outputStream = bluetoothSocket.getOutputStream();
            outputStream.write(1);
        } catch (IOException e) {
            e.printStackTrace();
        }}}

Calling this send gives me an error

gre_gor
  • 6,669
  • 9
  • 47
  • 52
24Doggi
  • 59
  • 3
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – luk2302 Jul 06 '20 at 20:39
  • @luk2302 the counter is not the problem. The connection is there. – 24Doggi Jul 06 '20 at 20:49
  • Ah, you are right. Look again at where you define your `bluetoothSocket`, do you define it once or twice? What scope does each declaration have, do they shadow each other? – luk2302 Jul 06 '20 at 20:51
  • Ah i have the solution. I just had to use the same outputstream. Declaring it every time was the mistake. Thanks – 24Doggi Jul 06 '20 at 21:02

0 Answers0