Navigation throws null exception when returning to a page 2nd time

sultanuzzaman

Member
Joined
Sep 13, 2015
Messages
18
Programming Experience
1-3
I have a serial port controller (for sensing any object that passes through its transport module) connected to USB. It responses with either 0 or 1 after input command "t" when it senses any dropping or no dropping of object through the transport. The page CshDeposit5 contains the code. Start.xaml is the page the activity starts and ends up in CshDeposit5.xaml. TmpErrorMsg and TmpMsg are the pages to show the user what happened. Everything runs fine for the first time. But when it tries to come to this CshDeposit5.xaml 2nd time it throws exception on the follwoing highlighted codes:

CshDeposit5.xaml.cs
C#:
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; //Declared in Global.cs as serial port


            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
                    {
[B][COLOR=#ff0000]                        NavigationService.Navigate(new Uri("TmpMsg.xaml", UriKind.RelativeOrAbsolute)); //Throws Null exception 2nd time it comes here[/COLOR][/B]
                    }));
                }
                else
                {
                    if (x.IndexOf("0") >= 0)
		    {
                        Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
                        {
[COLOR=#ff0000][B]                        NavigationService.Navigate(new Uri("TmpErrorMsg.xaml", UriKind.RelativeOrAbsolute)); [/B][/COLOR][B][COLOR=#FF0000]//Throws Null exception 2nd time it comes here[/COLOR][/B]
                        }));
                    }
                }
            }




	}


        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)); [B][COLOR=#008000]//No Error whatsoever[/COLOR][/B]
                })); 


                
            }


        }


    }
}


 
Last edited by a moderator:
It is easy to fix the Null exception, all you have to do is to check if any reference on that line could be Null, and the only reference on that line is NavigationService.

Then comes the time to find out why that reference is Null, at some time you are trying to use NavigationService when that service is not available to use.
 
Could it be in the following line?

Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate

Please note that nothing goes wrong when the CshDeposit5.xaml is called for the 1st time but problem arises when it is called the 2nd time.

Moreover, similar navigation is done from dispatcherTimer_Tick event and there's no throwing of null exception.


It is easy to fix the Null exception, all you have to do is to check if any reference on that line could be Null, and the only reference on that line is NavigationService.

Then comes the time to find out why that reference is Null, at some time you are trying to use NavigationService when that service is not available to use.
 
Last edited:
You need to debug and find out what object/page instance that throws the exception and at what time it does that, ie before the navigation is ready or possibly after page has navigated away if there are still events in motion. You could use date stamps to see the flow of what is going on and at what times if it is complex to figure out.
 
Does threading (with Dispatcher Invoke) have anything to with this? Please note that the navigation service used in Timer tick event works perfectly everytime. However navigation in the dispatcher invoke event used otherwise only works the 1st time. Can't figure out how to trace this bug.
 
Last edited:

Latest posts

Back
Top Bottom