Click here to Skip to main content
15,891,513 members
Home / Discussions / C#
   

C#

 
GeneralRe: Read Records then Count Pin
Agent__00715-Oct-15 4:05
professionalAgent__00715-Oct-15 4:05 
GeneralRe: Read Records then Count Pin
Richard Deeming15-Oct-15 4:10
mveRichard Deeming15-Oct-15 4:10 
GeneralRe: Read Records then Count Pin
Agent__00715-Oct-15 4:17
professionalAgent__00715-Oct-15 4:17 
GeneralRe: Read Records then Count Pin
macca2415-Oct-15 4:16
macca2415-Oct-15 4:16 
GeneralRe: Read Records then Count Pin
Agent__00715-Oct-15 17:58
professionalAgent__00715-Oct-15 17:58 
GeneralRe: Read Records then Count Pin
macca2427-Oct-15 5:49
macca2427-Oct-15 5:49 
GeneralRe: Read Records then Count Pin
Agent__00727-Oct-15 16:41
professionalAgent__00727-Oct-15 16:41 
QuestionWhy does Navigation throw null exception when returning to a page 2nd time Pin
Sultan Uz Zaman13-Oct-15 19:30
Sultan Uz Zaman13-Oct-15 19:30 
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:

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


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)); //No Error whatsoever
        })); 

        }

      }


    }
}


modified 14-Oct-15 4:31am.

SuggestionRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
Richard MacCutchan13-Oct-15 21:41
mveRichard MacCutchan13-Oct-15 21:41 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
Sultan Uz Zaman13-Oct-15 22:42
Sultan Uz Zaman13-Oct-15 22:42 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
Richard MacCutchan13-Oct-15 22:45
mveRichard MacCutchan13-Oct-15 22:45 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
Sultan Uz Zaman13-Oct-15 22:57
Sultan Uz Zaman13-Oct-15 22:57 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
Richard MacCutchan13-Oct-15 23:22
mveRichard MacCutchan13-Oct-15 23:22 
AnswerRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
NeillJam14-Oct-15 0:24
NeillJam14-Oct-15 0:24 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
Sultan Uz Zaman14-Oct-15 1:42
Sultan Uz Zaman14-Oct-15 1:42 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
NeillJam14-Oct-15 1:46
NeillJam14-Oct-15 1:46 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
Sultan Uz Zaman14-Oct-15 20:21
Sultan Uz Zaman14-Oct-15 20:21 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
NeillJam14-Oct-15 20:55
NeillJam14-Oct-15 20:55 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
Sultan Uz Zaman14-Oct-15 21:40
Sultan Uz Zaman14-Oct-15 21:40 
GeneralRe: Why does Navigation throw null exception when returning to a page 2nd time Pin
NeillJam15-Oct-15 20:20
NeillJam15-Oct-15 20:20 
Questionis this the Best method to get prime numbers between a range Pin
Sujith Karivelil13-Oct-15 18:54
professionalSujith Karivelil13-Oct-15 18:54 
AnswerRe: is this the Best method to get prime numbers between a range Pin
OriginalGriff13-Oct-15 20:12
mveOriginalGriff13-Oct-15 20:12 
GeneralRe: is this the Best method to get prime numbers between a range Pin
Sujith Karivelil13-Oct-15 20:57
professionalSujith Karivelil13-Oct-15 20:57 
QuestionC# desktop app being shared Pin
classy_dog13-Oct-15 7:15
classy_dog13-Oct-15 7:15 
AnswerRe: C# desktop app being shared Pin
Richard MacCutchan13-Oct-15 8:00
mveRichard MacCutchan13-Oct-15 8:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.