IOS App crash with Null Reference Exception at SetupCaptureSession()
See original GitHub issueI am in the process of updating a native xamarin ios app to a Microsoft.IOS .Net 6 app. I am using the ZXing.Net6.Mobile nuget which I used to replace the older ZXing.Net.Mobile .
The code being used was seamless as far as building but I am getting a null reference exception when calling the .Scan method. It’s crashing all the way back to the Main application file. Just before the crash the camera does try to open.
This is the stack trace I am getting object reference not set to instance of an object at ZXing.Mobile.ZXingScannerView.SetupCaptureSession() at ZXing.Mobile.ZXingScannerView.<StartScanning>b__42_0() at Foundation.NSActionDispatcher.Apply() at Foundation.NSObject.InvokeOnMainThread(Action ) at ZXing.Mobile.ZXingScannerView.StartScanning(Action`1 scanResultHandler, MobileBarcodeScanningOptions options) at ZXing.Mobile.ZXingScannerViewController.<ViewDidAppear>b__33_1() at Foundation.NSAsyncActionDispatcher.Apply() at UIKit.UIApplication.UIApplicationMain(Int32 , String[] , IntPtr , IntPtr ) at UIKit.UIApplication.Main(String[] , Type , Type ) at DSDRouteManager.iOS.Application.Main(String[] args) in /Users/jamespridgen/Desktop/DSDRouteManager/DSDRouteManager.IOS/Main.cs:line
These are the keys I have in Info.plist <key>NSCameraUsageDescription</key> <string>Can we use your camera</string> <key>Privacy - Camera Usage Description</key> <string>Taking picture barcode</string> <key>Privacy - Photo Library Usage Description</key> <string>Taking picture barcode</string>
This is the code I am using to call the camera
var auth = AVCaptureDevice.GetAuthorizationStatus(AVAuthorizationMediaType.Video);
if (auth == AVAuthorizationStatus.NotDetermined)
{
if (AVCaptureDevice.RequestAccessForMediaTypeAsync(AVAuthorizationMediaType.Video).Result)
{
auth = AVCaptureDevice.GetAuthorizationStatus(AVAuthorizationMediaType.Video);
}
}
// camera access has been granted at this point
var cameraScanner = new MobileBarcodeScanner(this); // "this" is the UIViewController
var options = Util.GetMobileBarcodeScanningOptions();
var result = await cameraScanner.Scan(options,true); // crash happens here
Scanning options
return new MobileBarcodeScanningOptions()
{
AutoRotate = true,
PossibleFormats = new List<BarcodeFormat>()
{
BarcodeFormat.All_1D,
BarcodeFormat.QR_CODE,
BarcodeFormat.CODE_39,
BarcodeFormat.CODE_128,
BarcodeFormat.UPC_A,
BarcodeFormat.UPC_E
}
};
Do I need some kind of initialization that the older nuget didn't require.
Any help would be much appreciated
James
Issue Analytics
- State:
- Created 3 months ago
- Comments:6

Top Related StackOverflow Question
@spinspin Thanks for this. It helped a bunch!
@jamesingreersc this should be enough to get going , I’m using MvvmCross but the core functionality to create a barcode scanner is in here
public class BarcodeScannerViewController : MvxViewController<BarcodeScannerViewModel> { AVCaptureSession captureSession; AVCaptureVideoPreviewLayer previewLayer; public Action<string> BarcodeScanned { get; set; }