0

I am using the fanatisc libary for scanning but I appear to have a new problem. Its crashing out on me after I set my last variable and I do not no why. I think it may have something to do with threads as to the reason why its crashing

private async void BtnTestScan_Clicked(object sender, EventArgs e)
{

        var scanPage = new ZXingScannerPage();
        scanPage.ToggleTorch();
        scanPage.IsScanning = true;
        // Navigate to our scanner page
        await Navigation.PushAsync(scanPage);


        scanPage.OnScanResult += (result) =>
        {


            // Stop scanning
            scanPage.IsScanning = false;

            // Pop the page and show the result
            Device.BeginInvokeOnMainThread(async () =>
            {
                BomComponentData getInfo = new BomComponentData();


                await Navigation.PopAsync();
                if (isBomScan.IsToggled == false)
                {
                    getInfo = await database.GetProductInfoByBarCode(result.Text);


                    lblOpertationName.Text = "Operation Name: " + getInfo.OperationName;
                    //var popup1 = new FuelStockApp.Views.PopupInputBox("You scanned the item " + getInfo.Name + "You need to pick " + getInfo.Quantity.ToString(), Convert.ToInt16(getInfo.Quantity));
                    BarCode = result.Text;
                    //await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(popup1);
                    Int32.TryParse(txtQtyUpdate.Text, out int Qty);
                    lblQty.Text = Qty.ToString();
                    gridItems.IsVisible = false;
                    txtCode.Text = BomId;
                    lblOpertationName.Text = "Operation Name: " + getInfo.OperationName;
                    lblBinLocation.Text = "Bin Location : " + getInfo.BinLocation.ToString();
                    lblBinLocationLabel.Text = "Bin Name : " + getInfo.BinName.ToString();

                    QtyToDeduct = Qty;
                }

}

I think it may be the way that the libary calls BeginInvoiteOnMainThread.

I have setup correctly according to documentation with the following in my mainactivy.cs

public class MainActivity : 

global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            Rg.Plugins.Popup.Popup.Init(this,savedInstanceState);

            LoadApplication(new App());
            }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

It crashing right after my onappearing event gets finished. Its all just standard stuff there.

protected async override void OnAppearing()
{
         base.OnAppearing();


        await database.copydb();

        List<BomComponentData> _boms = new List<BomComponentData>();


        _boms = await database.GetBomsAsync(BomId);
        viewModel.Boms = _boms.ToObservableCollection();
        viewModel.LoadItemsCommand.Execute(null);
        warehouses.RemoveAll (x => x.WarehouseID > 0);
        PickerItems.Clear();
        PickerItemList.Clear();
        warehouses = await database.GetWarehousesFromSage();
        if (warehouses.Count > 0)
        {
            viewModel.Warehouses = warehouses.ToObservableCollection();
            foreach (var warehouse in warehouses)
            {
                if (warehouse.Description == "")
                    warehouse.Description = warehouse.Name;


                PickerItems.Add(warehouse.WarehouseID.ToString(), warehouse.Description);

            }
            pickWarehouse.ItemsSource = PickerItemList;
        }

}

Unhandled Exception:

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

Thats all i get I don get any further information on the error.

Edit 2

I found that the exception is hapening here in my keyvalue pair and not zxing. SO I adjusted the title it appears to be here.

  private async void PickWarehouse_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnTestScan.IsVisible = true;
        gridItems.IsVisible = true;
        txtCode.IsVisible = true;


        KeyValuePair<string, string> selectedEntry= (KeyValuePair<string, string>)pickWarehouse.SelectedItem;

        List<BomComponentData> _boms = new List<BomComponentData>();


        WarehouseId = long.Parse(selectedEntry.Key);
        _boms = await database.GetBomsAsync(txtCode.Text);
        viewModel.Boms = _boms.ToObservableCollection();
        gridItems.ItemsSource = _boms;


    }

At this line in particualr

 KeyValuePair<string, string> selectedEntry= (KeyValuePair<string, string>)pickWarehouse.SelectedItem;
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • Have you used a try/catch to pinpoint the line throwing the error? – Andrew May 27 '19 at 20:58
  • @Andrew it comes as a unhanded exception is do you no how I acehive that in xamrian forms. – c-sharp-and-swiftui-devni May 27 '19 at 21:04
  • ALL exceptions are unhandled if you don't have any try/catch logic in your code. – Jason May 27 '19 at 21:26
  • @Jason i found the error by hitting the play button and seeing it in the output window it was to do with the fact that the picker would be null and I was not handling that as the case. – c-sharp-and-swiftui-devni May 27 '19 at 21:33
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dour High Arch May 27 '19 at 21:48
  • you are using PickerItemList as your ItemsSource, but it does not appear that you ever add any data to that. In OnAppearing you clear it and then never populated it again. You also have a variable named PickerItems - are you perhaps confusing the two? – Jason May 27 '19 at 22:06

0 Answers0