Opening ZXing Failed: System.NullReferenceException
See original GitHub issueHi All, I feel this might be a double up but i have not been able to find a solution for the below error. Please let me know if a solution has been found?
I have recently updated VS to 15.6.7 and subsequently updated to Xamrin.Forms 2.5.1.444934 with Zxing.Net.Mobile 2.4.1 using .NETStandard 2.0. Since doing this I now get a null reference error each time I call the script.
Also I have only seen the error on Android (testing iOS tonight)
Here is the error:
***** ERROR ***** BtnScanQR_Clicked - Opening ZXing Failed: System.NullReferenceException: Object reference not set to an instance of an object. at ZXing.Mobile.MobileBarcodeScanner.GetContext (Android.Content.Context context) [0x0001f] in <57b37b10a314448c8d6b2b9155539100>:0 at ZXing.Mobile.MobileBarcodeScanner.Scan (Android.Content.Context context, ZXing.Mobile.MobileBarcodeScanningOptions options) [0x00015] in <57b37b10a314448c8d6b2b9155539100>:0 at ZXing.Mobile.MobileBarcodeScanner.Scan (ZXing.Mobile.MobileBarcodeScanningOptions options) [0x00001] in <57b37b10a314448c8d6b2b9155539100>:0 at ZXing.Mobile.MobileBarcodeScannerBase.Scan () [0x00006] in <5cc6d3dca85f4634ba661b03b5b196fa>:0 at App.MainPage+<BtnScanQR_Clicked>d__2.MoveNext () [0x00023] in C:\Users\Admin\source\repos\App\App\App\MainPage.xaml.cs:67
In my MainActivity.cs I call the following after my Forms.Int:
//QR code reader
global::ZXing.Net.Mobile.Forms.Android.Platform.Init();
And this is the script with in my click event on the MainPage.cs:
//Open QR scanner for code scanning
private async void BtnScanQR_Clicked(object sender, EventArgs e)
{
try
{
var scanner = new MobileBarcodeScanner();
var result = await scanner.Scan();
strAccessToken.Text = result.Text.Trim();
}
catch (Exception ex)
{
GlobalScript.DebugMessages("ERROR", "BtnScanQR_Clicked", "Opening ZXing Failed: " + ex);
}
}
Even rolling back to my original versions of each package I’m now still having the same error.
Any assistance would be appretiated
Issue Analytics
- State:
- Created 5 years ago
- Comments:12

Top Related StackOverflow Question
@knight1219
if you used mvvmcross 6 ?? add this code at MainActivity Xamarin.Forms.Forms.Init(this, bundle);
Hi All,
Sorry for the delay. I have managed to get this working by updating my on click action to the below;
//Open QR scanner for code scanningprivate async void BtnScanQR_Clicked(object sender, EventArgs e){try{var options = new MobileBarcodeScanningOptions{AutoRotate = false,UseFrontCameraIfAvailable = false,TryHarder = true};var overlay = new ZXingDefaultOverlay{TopText = "Please scan QR code",BottomText = "Align the QR code within the frame"};var QRScanner = new ZXingScannerPage(options,overlay);await Navigation.PushModalAsync(QRScanner);QRScanner.OnScanResult += (result) =>{// Stop scanningQRScanner.IsScanning = false;// Pop the page and show the resultDevice.BeginInvokeOnMainThread(() =>{Navigation.PopModalAsync(true);strAccessToken.Text = result.Text.ToUpper().Trim();//DisplayAlert("Scanned Barcode", result.Text, "OK");});};}catch (Exception ex){GlobalScript.SeptemberDebugMessages("ERROR", "BtnScanQR_Clicked", "Opening ZXing Failed: " + ex);}}Hope this works for you. Also if you find any improvements in the above please share with me.
Packages: Xamarin.Forms 2.5.1.52743 ZXing.Net.Mobile.Forms 2.3.2