Navigating to a page second time throws null exception

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
I am using a serial port device that checks whether any object has been passed thru its gate when it is open. It takes "t" as input and responds with "1" if the object completely passed thru or "0" if the object returned. Depneding on the response the UI navigates to different pages with messages. A 20 seconds timer is used to trace the timeout without anything being passed through.

It runs alright the first time but throws null exception on the 2nd time and so forth.

For your understanding find below an excerpt of the code. Start.xaml is the 1st page, CshDeposit5.xaml is the page where a Dispatcher is invoked, tmpMsg.xaml and tmpErrorMsg.xaml are the message screens for the users to know what happened.

CshDeposit5.xaml.cs
-----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Windows.Threading;


namespace NWCDM
{
public partial class CshDeposit5 : Page
{
private System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
private static int i;


public CshDeposit5()
{
InitializeComponent();
}


private void Page_Loaded(object sender, RoutedEventArgs e)
{
i = Global.xDropTime; //Stipulated Time for the object to pass through before timeout - declared in Global.cs


TimeLeft.Text = i.ToString();

dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);


Global.xController.WriteLine("t"); //serialport declared in Global.cs file


dispatcherTimer.Start();


SerialPort sp2 = Global.xController;


sp2.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);


}


public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{

SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();


Dispatcher.Invoke(DispatcherPriority.Send, new Action(() => DoTrans(indata)));
}


private void DoTrans(string x){


if (i >= 0)
{
if (x.IndexOf("1") >= 0)
{
dispatcherTimer.Stop();


Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
{
NavigationService.Navigate(new Uri("TmpMsg.xaml", UriKind.RelativeOrAbsolute));
}));
}
else
{
if (x.IndexOf("0") >= 0)
{
Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
{
NavigationService.Navigate(new Uri("TmpErrorMsg.xaml", UriKind.RelativeOrAbsolute));
}));
}
}
}




}


private void dispatcherTimer_Tick(object sender, EventArgs e)
{
if (i >= 0)
{
if (Global.VarLang == 0)
{
TimeLeft.Text = i.ToString();
}


i--;
}
else
{
dispatcherTimer.Stop();


Global.xController.WriteLine("r"); //Shutter Gate Closes after timeout


Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
{
NavigationService.Navigate(new Uri("TmpErrorMsg.xaml", UriKind.RelativeOrAbsolute));
}));

}


}


}
}


I am ready to answer any query that you may have in this regard.

regards

Sultan
 
Top Bottom