Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why does Navigation throw null exception when returning to a page 2nd time

Why does Navigation throw null exception when returning to a page 2nd time

Scheduled Pinned Locked Moved C#
csharpsharepointwpflinqquestion
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sultan Uz Zaman
    wrote on last edited by
    #1

    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
    
    L N 2 Replies Last reply
    0
    • S Sultan Uz Zaman

      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
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • L Lost User

        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.

        S Offline
        S Offline
        Sultan Uz Zaman
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • S Sultan Uz Zaman

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          So you need to find out why NavigationService.GetNavigationService(this); is returning null rather than a valid object.

          S 1 Reply Last reply
          0
          • L Lost User

            So you need to find out why NavigationService.GetNavigationService(this); is returning null rather than a valid object.

            S Offline
            S Offline
            Sultan Uz Zaman
            wrote on last edited by
            #5

            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?

            L 1 Reply Last reply
            0
            • S Sultan Uz Zaman

              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?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • S Sultan Uz Zaman

                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
                
                N Offline
                N Offline
                NeillJam
                wrote on last edited by
                #7

                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

                S 1 Reply Last reply
                0
                • N NeillJam

                  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

                  S Offline
                  S Offline
                  Sultan Uz Zaman
                  wrote on last edited by
                  #8

                  How do I check if the page is already loaded or not in WPF?

                  N 1 Reply Last reply
                  0
                  • S Sultan Uz Zaman

                    How do I check if the page is already loaded or not in WPF?

                    N Offline
                    N Offline
                    NeillJam
                    wrote on last edited by
                    #9

                    Its been a while but a quick search leads me to using CurrentSource of the NavigationService. Something like: NavigationWindow.CurrentSource.ToString());

                    S 1 Reply Last reply
                    0
                    • N NeillJam

                      Its been a while but a quick search leads me to using CurrentSource of the NavigationService. Something like: NavigationWindow.CurrentSource.ToString());

                      S Offline
                      S Offline
                      Sultan Uz Zaman
                      wrote on last edited by
                      #10

                      Currentsource property not available

                      N 1 Reply Last reply
                      0
                      • S Sultan Uz Zaman

                        Currentsource property not available

                        N Offline
                        N Offline
                        NeillJam
                        wrote on last edited by
                        #11

                        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

                        S 1 Reply Last reply
                        0
                        • N NeillJam

                          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

                          S Offline
                          S Offline
                          Sultan Uz Zaman
                          wrote on last edited by
                          #12

                          It means you don't have any clue to suggest. Thanks anyways.

                          N 1 Reply Last reply
                          0
                          • S Sultan Uz Zaman

                            It means you don't have any clue to suggest. Thanks anyways.

                            N Offline
                            N Offline
                            NeillJam
                            wrote on last edited by
                            #13

                            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

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups