0

I'm storing a List of Objects in App.cs because I want to persist the data while the user walks across other content pages(using navigation page). When he backs to page that has the Picker, I wish to have the same items on it but it crashs the app when setting ItemsSources again.

Page that contains picker:

public class Local
{
   public string cidade { get; set; }
   public int id { get; set; }
}

if (((App)App.Current).Backup.Count<1)
{
   try
   {
      using (WebClient browser = new WebClient())
      {
          Uri uriCidades = new Uri("xxxxx.php");
          jsonCidades = await browser.DownloadStringTaskAsync(uriCidades);
      }
      var ListaCidades = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Local>>(jsonCidades);
      PickerCidades.ItemsSource = ListaCidades; // it works as expected
      ((App)App.Current).Backup = ListaCidades;  // it works as expected
   }
   catch (Exception)
   {
      throw;
   }
}
else
{
    PickerCidades.ItemsSource = ((App)App.Current).Backup; // the app crashs here

}

This is in App.cs


public class Local
{
   public string cidade { get; set; }
   public int id { get; set; }
}
public List<Local> Backup = new List<Local>();

Error output:

System.NullReferenceException: Object reference not set to an instance of an object.
  at MasterDetailPageNavigation.ContactsPage.CarregaCidades () [0x0016a] in ContactsPage.xaml.cs:56
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
  at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <11f101b564894ca7af6c482ddc51c698>:0
  at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <11f101b564894ca7af6c482ddc51c698>:0
  at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <11f101b564894ca7af6c482ddc51c698>:0
  at at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.47(intptr,intptr)
Sandro Benevides
  • 581
  • 2
  • 14
  • "it crashes" is a useless description. What is the specific exception that causes the crash? – Jason Jan 16 '20 at 23:49
  • @Jason have edited with log but the main message is "System.NullReferenceException: Object reference not set to an instance of an object." – Sandro Benevides Jan 17 '20 at 01:33
  • first, you need to figure out which element is null, and then you need to figure out why – Jason Jan 17 '20 at 01:35

2 Answers2

0

If it throws a NullReferenceException on the line of code where you have the comment, then there are only 3 possibilities: PickerCidades is null, App is null, or App.Current is null. If you set a breakpoint on that line and pause there in the debugger, you should be able to mouse over each individual reference and see which one (or maybe more than one) is null.

Joe Irby
  • 649
  • 6
  • 7
0

I could fix the issue by adding x:FieldModifier="public static" to my picker tag. Now it works, no changes to the code behind.

Sandro Benevides
  • 581
  • 2
  • 14