Xamarin forms: System.NullReferenceException when using ZXing.Net.Mobile for scanning?

Sreejith Sree 1,251 Reputation points
2021-02-24T15:55:04.867+00:00

I am using the ZXing.Net.Mobile NuGet package for scanning barcode numbers. I have done everything as per this blog.

My Code

//Main Project Interface
public interface IQrScanningService  
{  
   Task<string> ScanAsync();  
}

//Android part implementation
[assembly: Dependency(typeof(XFBarcode.Droid.Services.QrScanningService))]  
namespace projectname.Droid.Services  
{  
    public class QrScanningService : IQrScanningService  
    {  
        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;  
        }  
    }  
}  

But when I execute I am getting System.NullReferenceException: 'Object reference not set to an instance of an object.'. What else I am missing here? I saw a similar thread here but don't how to download use a package from GitHub.

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-02-25T02:21:26.133+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The error is happening because you're using a null object scanner .

    Before using the plugin we need to initialize the plugin .

       MobileBarcodeScanner.Initialize(MainActivity.Instance.Application);  
         
         
       //In your MainActivity   
         
       public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity  
       {  
           public static MainActivity Instance;  
           protected override void OnCreate(Bundle savedInstanceState)  
           {  
               TabLayoutResource = Resource.Layout.Tabbar;  
               ToolbarResource = Resource.Layout.Toolbar;  
         
               base.OnCreate(savedInstanceState);  
               Instance = this;  
               Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                 
               global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
               LoadApplication(new App());  
           }  
         
       }  
    

    Or initialize directly in MainActivity OnCreate() method .

       protected override void OnCreate(Bundle savedInstanceState)  
       {  
           TabLayoutResource = Resource.Layout.Tabbar;  
           ToolbarResource = Resource.Layout.Toolbar;  
         
           base.OnCreate(savedInstanceState);  
           Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
         
         
           MobileBarcodeScanner.Initialize(Application); //initialize  here  
         
         
           global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
           LoadApplication(new App());  
       }  
    

    Refer to https://github.com/Redth/ZXing.Net.Mobile#usage .


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.