title: UWP C# How to Use Routine From Different Page
body: <p>I am intending to write a UWP with <code>navigationview</code> where I use <code>rs485</code> to communicate to some devices.</p>
  <p>I have a <code>DS3231 RTC</code>, which I have made it work previously in earlier app.</p>
  <p>My <code>SettingPage</code> is where I configure the time, when I set the time, i ran into error. <code>system.nullreferenceexception: 'object reference not set to an instance of an object.'</code></p>
  <p>My <code>startClock()</code>, <code>readDS3231</code> and <code>setDS3231</code> routines are  in <code>MainPage.xaml.cs</code>.
  My <code>datepicker</code> and <code>timepicker</code> routines are in <code>SettingsPage</code>.</p>
  <p>Please help to let me know where did I do wrong and how do I correct it.
  Actually I am quite confused with the <code>page</code> thing. I can't understand it.</p>
  <hr />
  <p>SettingsPage.xaml.cs</p>
  <pre><code>private void DatePicker_DateChanged(object sender, DatePickerValueChangedEventArgs e)
      {
          DateTimeSettings.SetSystemDateTime(e.NewDate.UtcDateTime);
  
          mainPage.SetTime_DS3231(e.NewDate.UtcDateTime);
      }
  
  private void TimePicker_TimeChanged(object sender, TimePickerValueChangedEventArgs e)
      {
          var currentDate = DateTime.Now.ToUniversalTime();
  
          var newDateTime = new DateTime(currentDate.Year,
                                         currentDate.Month,
                                         currentDate.Day,
                                         e.NewTime.Hours,
                                         e.NewTime.Minutes,
                                         e.NewTime.Seconds);
  
          DateTimeSettings.SetSystemDateTime(newDateTime);
  
          mainPage.SetTime_DS3231(newDateTime);
      }
  
  private void TimeZoneComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          TimeZoneInfo selectedTimeZone = (TimeZoneInfo)this.TimeZoneComboBox.SelectedItem;
          Zone.Text = selectedTimeZone.ToString();
          TimeZoneInfo.ConvertTime(DateTime.Now, selectedTimeZone);
      }
  
  private void UpdateRTC_Click(object sender, RoutedEventArgs e)
      {
          mainPage.SetTime_DS3231(DateTime.Now);
      }
  
  private void ReadRTC_Click(object sender, RoutedEventArgs e)
      {
          mainPage.GetTime_DS3231();
  
          ReadSec.Text = mainPage.readSec;
          ReadMin.Text = mainPage.readMin;
          ReadHour.Text = mainPage.readHour;
          ReadDay.Text = mainPage.readDay;
          ReadDate.Text = mainPage.readDate;
          ReadMth.Text = mainPage.readMth;
          ReadYear.Text = mainPage.readYear;
      }
  </code></pre>
  <hr />
  <p>MainPage.xaml.cs</p>
  <pre><code>public async void GetTime_DS3231()
      {
          /* DS3231 I2C SLAVE address */
          int SlaveAddress = 0x68;
  
          try
          {
              // Initialize I2C
              var Settings = new I2cConnectionSettings(SlaveAddress);
              Settings.BusSpeed = I2cBusSpeed.StandardMode;
  
              if (AQS == null || DIS == null)
              {
                  AQS = I2cDevice.GetDeviceSelector(&quot;I2C1&quot;);
                  DIS = await DeviceInformation.FindAllAsync(AQS);
              }
  
              //rtcError = false;
  
              using (I2cDevice Device = await I2cDevice.FromIdAsync(DIS[0].Id, Settings))
              {
                  byte[] writeBuf = { 0x00 };
                  Device.Write(writeBuf);
                  byte[] readBuf = new byte[7];
                  Device.Read(readBuf);
                  byte second = bcdToDec((byte)(readBuf[0] &amp; 0x7f));
                  byte minute = bcdToDec(readBuf[1]);
                  byte hour = bcdToDec((byte)(readBuf[2] &amp; 0x3f));
                  byte dayOfWeek = bcdToDec(readBuf[3]);
                  byte dayOfMonth = bcdToDec(readBuf[4]);
                  byte month = bcdToDec(readBuf[5]);
                  byte year = bcdToDec(readBuf[6]);
  
                  readSec = second.ToString();
                  readMin = minute.ToString();
                  readHour = hour.ToString();
                  readDay = dayOfWeek.ToString();
                  readDate = dayOfMonth.ToString();
                  readMth = month.ToString();
                  readYear = year.ToString();
                  
                  var currentDate = DateTime.Now.ToUniversalTime();
  
                  var newDateTime = new DateTime(currentDate.Year,
                                         month,
                                         dayOfMonth,
                                         hour,
                                         minute,
                                         second);
  
                  DateTimeSettings.SetSystemDateTime(newDateTime);
  
              }
          }
          catch (Exception ex)
          {
              MainStatusDisplay.Text = ex.Message;
              //System.Diagnostics.Debug.Write(ex.Message);
          }
      }
  
      public async void SetTime_DS3231(DateTime dt)
      {
          int SlaveAddress = 0x68;
  
          try
          {
              // Initialize I2C
              var Settings = new I2cConnectionSettings(SlaveAddress);
              Settings.BusSpeed = I2cBusSpeed.StandardMode;
  
              if (AQS == null || DIS == null)
              {
                  AQS = I2cDevice.GetDeviceSelector(&quot;I2C1&quot;);
                  DIS = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(AQS);
              }
  
              //rtcError = false;
  
              using (I2cDevice Device = await I2cDevice.FromIdAsync(DIS[0].Id, Settings))
              {
                  byte write_seconds = decToBcd((byte)dt.Second);
                  byte write_minutes = decToBcd((byte)dt.Minute);
                  byte write_hours = decToBcd((byte)dt.Hour);
                  byte write_dayofweek = decToBcd((byte)dt.DayOfWeek);
                  byte write_day = decToBcd((byte)dt.Day);
                  byte write_month = decToBcd((byte)dt.Month);
                  //byte write_year = decToBcd((byte)dt.Year);
                  byte write_year = IntToBcd(dt.Year % 100);
  
                  byte[] write_time = { 0x00, write_seconds, write_minutes, write_hours, write_dayofweek, write_day, write_month, write_year };
  
                  Device.Write(write_time);
  
              }
          }
          catch (Exception ex)
          {
              //rtcError = true;
              MainStatusDisplay.Text = ex.Message;
              //System.Diagnostics.Debug.Write(ex.Message);
          }
  
      }
  </code></pre>
  <hr />
  <p><a href="https://i.stack.imgur.com/kdiUX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kdiUX.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/5NaGL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5NaGL.jpg" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/rCzNp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rCzNp.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/2yBnF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2yBnF.png" alt="enter image description here" /></a></p>