2

I have this code:

[Table ("address_info")]
public class AddressInfoDB
{
    [PrimaryKey, Unique]
    public int stt { set; get; }
    public int customerAddressId { set; get; }
    public string address { set; get; }
    public string phone { set; get; }
    public int cityId { set;get; }
    public int districtId { set; get; }
    public int wardId { set; get; }
    public string cityName { set; get; }
    public string districtName { set; get; }
    public string wardName { set; get; }
    public string fullAddress { set; get; }
    public int isMainAddress { set; get; }      

    public override string ToString ()
    {
        return this.fullAddress;
    }
}

public class AddressSenderInList
{
    public ObservableCollection<AddressInfoDB> itemsList;
    public AddressSenderInList()
    {
        this.itemsList = new ObservableCollection<AddressInfoDB> ();
    }

}

and code to get data from SQLite to fill list view in content page

listItems = new AddressSenderInList();

listView = new ListView {
    SeparatorVisibility = SeparatorVisibility.None,
    HasUnevenRows = true,
    ItemTemplate = new DataTemplate(() => new AddressInfoCell(this)),
    VerticalOptions = LayoutOptions.FillAndExpand,
    HorizontalOptions = LayoutOptions.Fill,
    IsPullToRefreshEnabled = true,
    ItemsSource = listItems.itemsList,
};

        fillListAddress ();

code GetAllAddressInfo in SQLite Controller

  public async Task<List<AddressInfoDB>> GetAllAddressInfo(){
        var lists = await dbConn.Table<AddressInfoDB> ().ToListAsync ();
        return lists;
  }

code fillListAddress function

public async void fillListAddress(){
    var abc = await App.dbManager.GetAllAddressInfo ();
    Debug.WriteLine ("Data from SQLite: "+abc.Count);
    if (listItems.itemsList == null) {
        Debug.WriteLine ("ItemSource Null?");
    } else {
        Debug.WriteLine ("ItemSource not null: "+listItems.itemsList.Count);
        foreach (var item in abc) {
            listItems.itemsList.Add (item);
        }
    }
}

But I get error

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

I'm sure code get data from SQLite is correct..

Output print:

Data from SQLite: 2
ItemSource not null: 0
bulubuloa
  • 577
  • 1
  • 9
  • 20
  • At which line this exception is throwing.. – ssilas777 Sep 01 '15 at 08:52
  • listItems.itemsList.Add (item); – bulubuloa Sep 01 '15 at 08:55
  • @HoangQBH: May I suggest that you change the signature of `fillListAddress` to `public async Task FillListAddressAsync()`, and then `await` it where you're calling it: `await FillListAddressAsync();` instead of `fillListAddress();`. – stakx - no longer contributing Sep 01 '15 at 09:53
  • @HoangQBH: Please note that your answer cannot be answered with certainty. It is not 100% clear whether `listItems` in `listItems = new AddressSenderInList();` refers to the same field or property as `listItems` inside `fillListAddress`. Perhaps check that you do assign to the same field. Make especially sure that you are not assigning to a local variable of the same name in your 2nd code block. – stakx - no longer contributing Sep 01 '15 at 10:05

0 Answers0