0

I'm writing a barcode scanner app in Xamarin Forms and I'm using the ZXing library to handle barcode scanning and parsing. The following code works perfectly, bringing up an alert when the code is scanned:

OnScanResult += (result) => {
            IsScanning = false;

            var parsedResult = ResultParser.parseResult(result);

            Device.BeginInvokeOnMainThread(() => {
                Navigation.PopAsync();
                DisplayAlert("Barcode Info", parsedResult.DisplayResult, "OK");
            });

However, I want to use the ExpandedProductParsed result type for this method and harvest the productID field, so I tried this:

OnScanResult += (result) => {
            IsScanning = false;

            var productResult = ResultParser.parseResult(result) as ExpandedProductParsedResult;

            Device.BeginInvokeOnMainThread(() => {
                Navigation.PopAsync();
                DisplayAlert("Barcode Info", productResult.ProductID, "OK");
            });
        };

For some reason this parse doesn't work and it ends up throwing a null reference exception. Why does the first instance work but not the second one?

0 Answers0