Home > Net >  Exception : System.NullReferenceException: 'Object reference not set to an instance of an objec
Exception : System.NullReferenceException: 'Object reference not set to an instance of an objec

Time:05-18

I'm trying to create a simple Qr code scanner application who have a single view like this: enter image description here

But when I tried to press the "CLICK" button to lunch the scanner I got this excption:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

this is the main.xaml page:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="scanner.MainPage">

        <StackLayout Spacing="10">
            <Button Text="Click"  
                x:Name="btnScan"  
                Clicked="btnScan_Clicked"/>
            <Entry x:Name="txtBarcode"  
               Placeholder="Text Do scan"/>
        </StackLayout>

</ContentPage>

the main.xaml.cs:

 public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void btnScan_Clicked(object sender, EventArgs e)
        {
            try
            {
                var scanner = DependencyService.Get<Interface1>();
                var result = await scanner.ScanAsync();
                if (result != null)
                {
                    txtBarcode.Text = result;
                }
            }
            catch (Exception ex)
            { throw;
            }

the service:

  internal class QrScanningService : Interface1
    {
        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;
        }
    }

CodePudding user response:

I had done a simple and meet the same problem. So I searched it on the internet and found that the plugin need to be initialized when you use the ZXing.Net.Mobile package.

So you can add the initialized code in to the OnCreate method of the MainActivity. Such as:

 protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        MobileBarcodeScanner.Initialize(Application);// this line initialize the plugin
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }

You can check the document about it :https://github.com/Redth/ZXing.Net.Mobile#android

And on the ios check this link:https://github.com/Redth/ZXing.Net.Mobile#ios

  • Related