locked
can't call page erro Object reference not set to an instance of an object RRS feed

  • Question

  • User395446 posted

    Greeting everyone i'm still newbie in Xamarin.form platform i'm trying to create StockCount app so i'm using c# markup on my app and everything was working fine but this page when i try to call it i got this error messages

    and this is my code for that page


    using SQLite;
    using StockCount.Models;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    
    using Xamarin.Forms;
    
    namespace StockCount.Views
    {
        public class Stocktaking : ContentPage
        {
            private Entry _barcodeEntry;
            private ListView _listViewStocking;
            public string NameST2;
            public string NumberST2;
            public string BarcodeST2;
            public int QtyST2;
    
            string _dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "StockCountDB.db3");
            public Stocktaking()
            {
                this.Title = "StokingPage";
    
                _barcodeEntry.Focus();
    
                var db = new SQLiteConnection(_dbPath);
    
                StackLayout stackLayout = new StackLayout();
    
                _barcodeEntry = new Entry();
                _barcodeEntry.Keyboard = Keyboard.Text;
                _barcodeEntry.Placeholder = "Barcode here";
                _barcodeEntry.Completed += _barcodeEntry_Completed; ;
                stackLayout.Children.Add(_barcodeEntry);
    
                _listViewStocking = new ListView();
                try
                {
                    _listViewStocking.ItemsSource = db.Table<StocktakingTB>().OrderBy(x => x.NameST).ToList();
                }
                catch
                {
    
                }
                stackLayout.Children.Add(_listViewStocking);
    
                Content = stackLayout;
            }
    
            private void _barcodeEntry_Completed(object sender, EventArgs e)
            {
                var db = new SQLiteConnection(_dbPath);
                string searchbarcodetext = _barcodeEntry.Text;
    
                if (db.Table<ProductsTB>().Count(o => o.BarcodeProduct == searchbarcodetext) == 0)
                {
                    DisplayAlert("Error", "not found Barcode", "OK");
                }
                else
                {
                    if(db.Table<StocktakingTB>().Count(o => o.BarcodeST == searchbarcodetext) == 0)
                    {
                        NameST2 = db.Table<ProductsTB>().Where(o => o.BarcodeProduct == searchbarcodetext).SingleOrDefault().ProductName;
                        NumberST2 = db.Table<ProductsTB>().Where(o => o.BarcodeProduct == searchbarcodetext).SingleOrDefault().ProductNumber;
                        BarcodeST2 = db.Table<ProductsTB>().Where(o => o.BarcodeProduct == searchbarcodetext).SingleOrDefault().BarcodeProduct;
    
                        db.CreateTable<StocktakingTB>();
    
                        StocktakingTB stocktaking = new StocktakingTB()
                        {
                            NameST = NameST2,
                            NumberST = NumberST2,
                            BarcodeST = BarcodeST2,
                            Qty = 1
                        };
    
                        db.Insert(stocktaking);
                        NameST2 = "";
                        NumberST2 = "";
                        BarcodeST2 = "";
                        _listViewStocking.ItemsSource = db.Table<StocktakingTB>().OrderBy(x => x.NameST).ToList();
                        _barcodeEntry.Text = "";
                        _barcodeEntry.Focus();
                    }
                    else
                    {
                        NameST2 = db.Table<ProductsTB>().Where(o => o.BarcodeProduct == searchbarcodetext).SingleOrDefault().ProductName;
                        NumberST2 = db.Table<ProductsTB>().Where(o => o.BarcodeProduct == searchbarcodetext).SingleOrDefault().ProductNumber;
                        BarcodeST2 = db.Table<ProductsTB>().Where(o => o.BarcodeProduct == searchbarcodetext).SingleOrDefault().BarcodeProduct;
                        //var db = new SQLiteConnection(_dbPath);
                        StocktakingTB stocktaking = new StocktakingTB()
                        {
                            NameST = NameST2,
                            NumberST = NumberST2,
                            BarcodeST = BarcodeST2,
                            Qty =+ 1
                        };
                        db.Update(stocktaking);
                    }
    
                }
    
            }
        }
    }
    
    -------------------------------------
    

    and this is table which i'll inserted to

    using SQLite;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace StockCount.Models
    {
        class StocktakingTB
        {
            public string NameST { get; set; }
            public string NumberST { get; set; }
            public string BarcodeST { get; set; }
            public decimal Qty { get; set; }
            public override string ToString()
            {
                return this.BarcodeST + "- " + this.NameST + "(" + this.Qty + ")";
            }
    
        }
    }
    
    Tuesday, July 7, 2020 9:35 AM

Answers

  • User89714 posted

    @mhmqi

    You are calling _barcodeEntry.Focus(); before _barcodeEntry = new Entry();

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 7, 2020 9:56 AM

All replies

  • User89714 posted

    @mhmqi

    You are calling _barcodeEntry.Focus(); before _barcodeEntry = new Entry();

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 7, 2020 9:56 AM
  • User395446 posted

    @JohnHardman said: @mhmqi

    You are calling _barcodeEntry.Focus(); before _barcodeEntry = new Entry();

    yes this is solve this problem but how can I make it focus on that entry directly when open that page?!

    Wednesday, July 8, 2020 6:42 AM
  • User89714 posted

    @mhmqi said: yes this is solve this problem but how can I make it focus on that entry directly when open that page?!

    To do it in a way that works across all target platforms, you need to call Focus() only after the Entry has been rendered.

    One way to do this (which admittedly is a bit nasty) is to put the following in an OnAppearing() override:

    Task.Delay(500).ContinueWith(t => { Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(async () => { if (_barcodeEntry != null) _barcodeEntry.Focus(); }); }, TaskContinuationOptions.OnlyOnRanToCompletion);

    I haven't compiled that, so forgive any typo's.

    Wednesday, July 8, 2020 9:43 AM