question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Opening ZXing Failed: System.NullReferenceException

See original GitHub issue

Hi 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:open
  • Created 5 years ago
  • Comments:12

github_iconTop GitHub Comments

15reactions
suyanacommented, Jun 15, 2018

@knight1219

if you used mvvmcross 6 ?? add this code at MainActivity Xamarin.Forms.Forms.Init(this, bundle);

10reactions
Magnabattcommented, Jun 8, 2018

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 scanning private 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 scanning QRScanner.IsScanning = false;

// Pop the page and show the result Device.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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Xamarin forms: System.NullReferenceException when ...
I am using the ZXing. ... NullReferenceException when using ZXing. ... The error is happening because you're using a null object scanner ....
Read more >
ZXing Barcode Scanner NullReferenceException with ...
I am getting this error when I try to click the button to scan it: System.NullReferenceException: Object reference not set to an instance...
Read more >
ZXing.NET Generate QR Code & Barcode in C# Alternatives
ZXing.NET is a library that decodes and generates barcodes (like QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar). ZXing, which stands ......
Read more >
(Android/Kotlin) Conflicting Dependencies error when ...
I am using PdfDocument class for PDF generation, com.github.yuriy-budiyev:code-scanner:2.3.2 dependency for scanning QR Code, and the Zxing ...
Read more >
a QR/barcode scanner app with Delphi, ZXing and TFrameStand
The ZXing project is the most popular open source barcode scanning library in Java and has been ported to different languages (C++, ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found