Shairyar
Shairyar

Reputation: 3356

Flutter plugin - Attempt to invoke virtual method on a null object reference

I am learning how to create Flutter Plugins so I decided to create a Bluetooth plugin, I know there are many available out there but the purpose here to learn.

The error I am facing is as following

E/MethodChannel#flutterpluginbluetooth(22072): Failed to handle method call
E/MethodChannel#flutterpluginbluetooth(22072): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)' on a null object reference

I am not a Java developer and this I am sure is not the pretty approach so just ignore the approach for now.

This is my onMethodCall

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if (call.method.equals("getMessage")) {
      BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      result.success("hello");


      System.out.println("DEBUG - Starting discovery");
      bluetoothAdapter.startDiscovery();
      final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
          System.out.println("DEBUG - inside onReceive");
          String action = intent.getAction();
          System.out.println("DEBUG - Action is: " + action);
          //Finding devices
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            System.out.println("Device Name: "  + device.getName());
          } else {
            System.out.println("DEBUG: Action did not match"  + BluetoothDevice.ACTION_FOUND);
          }
        }

      };
      IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
      System.out.println("DEBUG - About to registerReceiver");
      context.registerReceiver(mReceiver, filter);


    }
    else {
      result.notImplemented();
    }

  }

The question I have is why I am getting an error on this context.registerReceiver(mReceiver, filter);

I am also reading the android documentation and that is calling registerReceiver without any context and if I try that approach I get an error as well which is as following which occurs during the compile time.

error: cannot find symbol registerReceiver(mReceiver, filter);

This is onAttachedToEngine

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutterpluginbluetooth");
    channel.setMethodCallHandler(this);
  }

Regarding Context

I am just importing it

import android.content.Context;

and inside a class I am declaring a context variable

private Context context;

I will really appreciate some help here.

Upvotes: 1

Views: 4763

Answers (1)

Abed Elaziz Shehadeh
Abed Elaziz Shehadeh

Reputation: 322

It doesn't seem you're initilasing context, you can do that using flutterPluginBinding inside onAttachedToEngine such as;

context = flutterPluginBinding.getApplicationContext();

Upvotes: 1

Related Questions