2015-02-24 79 views
-1

我目前正在爲Azure使用windows phone 8.1開發應用程序。 (根據我的經驗,我不會真的推薦給你)。無論如何...我有其中的多個控制器類,他們真的是這樣做的(檢查數據庫中的某些東西是否已經存在,如果沒有,就創建它)。我的問題必須是在檢查數據庫,如果一個條目已經存在的read()函數:Azure MobileService空引用異常

public async void Read(Device device) 
{ 
    IMobileServiceTable mobileServiceTable = Connect(); 
    MobileServiceCollection<Device, Device> devices = null; 
    try 
    { 
     devices = await mobileServiceTable.MobileServiceClient.GetTable<Device>().Where(d => d.Manufacturer == device.Manufacturer && d.Model == device.Model).ToCollectionAsync(); 
    } 
    catch (Exception e) 
    { 
     if (_onDeviceControllerListener != null) 
     { 
      _onDeviceControllerListener.OnError(ControllerError.Error.ReadFromDatabase, e.ToString()); 
     } 
      return; 
     } 

    if (_onDeviceControllerListener != null && devices != null) 
    { 
     _onDeviceControllerListener.OnRead(devices); 
    } 
} 

這一個完美的作品應該如何,但奧得一個這基本上只是一個副本拋出一個NullReferenceException由行「apps = await mobileServiceTab ...」:

public async void Read(Model.App app) 
{ 
    IMobileServiceTable mobileServiceTable = Connect(); 
    MobileServiceCollection<Model.App, Model.App> apps = null; 
    try { 
     apps = await mobileServiceTable.MobileServiceClient.GetTable<Model.App>().Where(a => a.HardwareId == app.HardwareId && a.PackageId == app.PackageId).ToCollectionAsync(); 
    } 
    catch (Exception e) 
    { 
     if (_onAppControllerListener != null) 
     { 
      _onAppControllerListener.OnError(ControllerError.Error.ReadFromDatabase, e.ToString()); 
     } 
     return; 
    } 

    if (_onAppControllerListener != null) 
    { 
     _onAppControllerListener.OnRead(apps); 
    } 
} 

有人知道問題是什麼嗎? 感謝您的幫助

+0

什麼是您的異常的堆棧跟蹤? – carlosfigueira 2015-02-24 17:26:56

+0

你可以通過它來告訴我們,當錯誤被拋出時哪個對象爲空? – Chris 2015-02-24 18:51:20

回答

0

您可以添加一個try ... catch塊並檢查「MobileServiceInvalidOperationException」異常。也看看這個callstack來了解發生了什麼。

如果您成功撥打移動服務以檢索數據,則提琴手跟蹤還會讓您知道。然後你可以確定問題在於如何處理代碼中的數據。

再看一下您的MobileServiceContext類,並確保所有屬性都被考慮在內,特別是使用「Model.App」類的屬性。例如

public virtual DbSet<Model.App> SomeProperty { get; set; } 

希望這會有所幫助。