Why does Navigation throw null exception when returning to a page 2nd time
-
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
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
-
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
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
Please edit your question, remove the blank lines, add proper indentation, and add <pre lang="c#"></pre> tags around your code to make it more readable, like this:
public CshDeposit5()
{
InitializeComponent();
}Next, you should use your debugger to identify exactly which variable is null when the exception is thrown.
-
Please edit your question, remove the blank lines, add proper indentation, and add <pre lang="c#"></pre> tags around your code to make it more readable, like this:
public CshDeposit5()
{
InitializeComponent();
}Next, you should use your debugger to identify exactly which variable is null when the exception is thrown.
NavigationService.Navigate(new Uri("TmpMsg.xaml", UriKind.RelativeOrAbsolute)); throws the null exception. I tried the following, NavigationService nav = NavigationService.GetNavigationService(this); nav.Navigate(new Uri("TmpMsg.xaml", UriKind.RelativeOrAbsolute)); where nav variable is shown null.
-
NavigationService.Navigate(new Uri("TmpMsg.xaml", UriKind.RelativeOrAbsolute)); throws the null exception. I tried the following, NavigationService nav = NavigationService.GetNavigationService(this); nav.Navigate(new Uri("TmpMsg.xaml", UriKind.RelativeOrAbsolute)); where nav variable is shown null.
-
So you need to find out why
NavigationService.GetNavigationService(this);
is returning null rather than a valid object.Please note as I mentioned earlier it doesn't thro any null exception 1st time. I get this when CshDeposit5.xaml is called the 2nd time. How do I find its returning null?
-
Please note as I mentioned earlier it doesn't thro any null exception 1st time. I get this when CshDeposit5.xaml is called the 2nd time. How do I find its returning null?
Unfortunately that is the nature of code bugs, they only happen when you least expect them. The only way to track the problem is by debugging and testing. You should examine all the information in the exception including the stack trace, in order to try and find out what has happened leading up to the error.
-
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
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
Good day. Please fix the formatting. It makes it really hard to read, so I have not read through your code. Anyway, the first thing that comes to mind is check that when you try to navigate to a XAML page a second time, the NavigationService.Navigate method already has the page you are trying to navigate to in memory. Something similar happened to me quote some time ago. I fixed it by first checking if the page I wanted to navigate to was not already loaded into the NavigationService. If it was, I didn't do anything, else let the Navigation proceed. Hope that helps. Neill
-
Good day. Please fix the formatting. It makes it really hard to read, so I have not read through your code. Anyway, the first thing that comes to mind is check that when you try to navigate to a XAML page a second time, the NavigationService.Navigate method already has the page you are trying to navigate to in memory. Something similar happened to me quote some time ago. I fixed it by first checking if the page I wanted to navigate to was not already loaded into the NavigationService. If it was, I didn't do anything, else let the Navigation proceed. Hope that helps. Neill
How do I check if the page is already loaded or not in WPF?
-
How do I check if the page is already loaded or not in WPF?
-
Its been a while but a quick search leads me to using CurrentSource of the NavigationService. Something like: NavigationWindow.CurrentSource.ToString());
Currentsource property not available
-
Currentsource property not available
-
Like I said, its only a possibility that that is causing your problem. Do some research to try and figure out how to get the CurrentSource, we can't do everything for you. Neill
It means you don't have any clue to suggest. Thanks anyways.
-
It means you don't have any clue to suggest. Thanks anyways.
After reviewing some more, it is quite likely that the code is losing reference to the NavigationService object due to DoTrans() being run on a different thread. But hey, maybe I just don't have any clue to suggest. Good luck in finding the problem. Neill