Xamarin forms: System.NullReferenceException when using ZXing.Net.Mobile for scanning?

683 views Asked by At

I am using the ZXing.Net.Mobile NuGet package for scanning barcode numbers. I have done everything as per this blog.

My Code

//Main Project Interface
public interface IQrScanningService  
{  
   Task<string> ScanAsync();  
}

//Android part implementation
[assembly: Dependency(typeof(XFBarcode.Droid.Services.QrScanningService))]  
namespace projectname.Droid.Services  
{  
    public class QrScanningService : IQrScanningService  
    {  
        public async Task<string> ScanAsync()  
        {  
            var optionsDefault = new MobileBarcodeScanningOptions();  
            var optionsCustom = new MobileBarcodeScanningOptions();  
  
            var scanner = new MobileBarcodeScanner()  
            {  
                TopText = "Scan the QR Code",  
                BottomText = "Please Wait",  
            };  
  
            var scanResult = await scanner.Scan(optionsCustom);  
            return scanResult.Text;  
        }  
    }  
}  

But when I execute I am getting System.NullReferenceException: 'Object reference not set to an instance of an object.'. What else I am missing here? I saw a similar thread here but don't how to download use a package from GitHub.

1

There are 1 answers

1
Leo Zhu On BEST ANSWER

You missed the initialization step.

Try this:

public async Task<string> ScanAsync()
    {
        //Initialize the scanner first so it can track the current context
        MobileBarcodeScanner.Initialize(MainActivity.Instance.Application);

        var optionsDefault = new MobileBarcodeScanningOptions();
        var optionsCustom = new MobileBarcodeScanningOptions();

        var scanner = new MobileBarcodeScanner()
        {
            TopText = "Scan the QR Code",
            BottomText = "Please Wait",
        };

        var scanResult = await scanner.Scan(optionsCustom);
        return scanResult.Text;
    }  

in your MainActivity :

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static MainActivity Instance;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
        Instance = this;
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }

}

You also could initialize in MainActivity OnCreate() method directly.

protected override void OnCreate(Bundle savedInstanceState)
{
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);
    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    MobileBarcodeScanner.Initialize(Application); //initialize  here
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    LoadApplication(new App());
}