1
Vote

Null exception crash

description

Firstly I would like to thank you guys for providing such a wonderful library. This has indeed helped is delivering our users better user experience.

We have incorporated your library in our app. But recently we are facing a particular crash. The stack trace is as follows:

System.NullReferenceException: Object reference not set to an instance of an object.
at Coding4Fun.Toolkit.Controls.Common.ApplicationSpace.get_CurrentDispatcher()
at Coding4Fun.Toolkit.Controls.ToastPrompt.<TimerTick>d__4.MoveNext()

We are using the following code to display the toast. Even though we have used a try catch, its not being handled.

try
            {
                ToastPrompt toast = new ToastPrompt();
                toast.Tag = param;
                toast.Title = title;
                // Toast ui doesn't appear correct when there is new line in text.
                int indexOfNewLine = content.IndexOf(_newLineCharNonUnixPlatform);
                if (indexOfNewLine < 0)
                    indexOfNewLine = content.IndexOf(_newLineCharUnixPlatform);
                toast.Message = indexOfNewLine > 0 ? content.Substring(0, indexOfNewLine) : content;
                toast.Foreground = new SolidColorBrush(Colors.White);
                toast.Background = (SolidColorBrush)App.Current.Resources["Blue"];
                toast.VerticalContentAlignment = VerticalAlignment.Center;
                toast.ImageSource = Utilities.ImageUtility.Logo;
                toast.MaxHeight = 60;
                toast.Tapped += ToastTapped;
                toast.Show();
            }
            catch (Exception ex)
            {
                //Coding toolkit throws internal exception
                Debug.WriteLine("ForegroundNotificationManager::GenerateInAppToast:Exception:" + ex.Message);
            }
Could you help us in providing a fix for the null excpetion? Also guide us in how to handle this crash more gracefully?

comments

crutkas wrote Dec 24, 2015 at 5:08 PM

  • Where are you calling this? Based on the error, I know what is happening but want to confirm.
  • can you provide a repro, this will help with both exactly figuring out the issue + give you a solution to what is happening.
  • what is style app is this? UWP, universal, silver light?

rg912 wrote Dec 28, 2015 at 7:53 AM

Hi @crutkas,
1)We are calling from windows phone win rt 8.1 app from UI thread.
2)we dont have 100% repro, we have seen this in crash logs multiple times but we dont have in house repro.:(
3)Windows Phone 8.1 Rt app

Guide us how to handle this.

crutkas wrote Dec 29, 2015 at 4:48 AM

what version of the c4f toolkit are you using? can you create a base repro with how you're calling it?

rg912 wrote Dec 29, 2015 at 6:38 AM

We are using Coding4Fun.Toolkit.Controls.2.1.0\lib\wpa81\Coding4Fun.Toolkit.dll.

We are using this function to generate in app toast.

/// <summary>
    /// Function to generate in app toast.
    /// </summary>
    /// <param name="title">Title for the toast.</param>
    /// <param name="content">Content for the toast.</param>
    /// <param name="param">Param to be passed in toast.</param>
    private void GenerateInAppToast(string title, string content, string param)
    {

if WINDOWS_PHONE_APP

        HikeViewModel.CurrentDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            try
            {
                ToastPrompt toast = new ToastPrompt();
                toast.Tag = param;
                toast.Title = title;
                // Toast ui doesn't appear correct when there is new line in text.
                int indexOfNewLine = content.IndexOf(_newLineCharNonUnixPlatform);
                if (indexOfNewLine < 0)
                    indexOfNewLine = content.IndexOf(_newLineCharUnixPlatform);
                toast.Message = indexOfNewLine > 0 ? content.Substring(0, indexOfNewLine) : content;
                toast.Foreground = new SolidColorBrush(Colors.White);
                toast.Background = (SolidColorBrush)App.Current.Resources["HikeBlue"];
                toast.VerticalContentAlignment = VerticalAlignment.Center;
                toast.ImageSource = Utilities.ImageUtility.HikeLogo;
                toast.MaxHeight = 60;
                toast.Tapped += ToastTapped;
                toast.Show();
            }
            catch (Exception ex)
            {
                //Coding toolkit throws internal exception
                Debug.WriteLine("ForegroundNotificationManager::GenerateInAppToast:Exception:" + ex.Message);
            }
        });

endif

Let

crutkas wrote Dec 29, 2015 at 7:10 AM

  • Any more toward that crash stack trace?
  • i need something i can attach a debugger against. Can you attach a simple repro project i can download and unzip? And include anyway that toast call gets executed. Right now based on that I have zero clue where the code is actually getting executed or what possible code paths could call that method.
  • looking at the nuget, the current version is 2.1.7 but not sure if that will fix anything.

rg912 wrote Dec 29, 2015 at 8:43 AM

Looking at code http://coding4fun.codeplex.com/SourceControl/latest#source/Coding4Fun.Toolkit.Controls/Prompts/ToastPrompt.cs:

async void TimerTick(object state)
   {

if WINDOWS_STORE || WINDOWS_PHONE_APP

       await ApplicationSpace.CurrentDispatcher.RunAsync(CoreDispatcherPriority.Normal, 

elif WINDOWS_PHONE

       ApplicationSpace.CurrentDispatcher.BeginInvoke(

endif

           () => OnCompleted(new PopUpEventArgs<string, PopUpResult> { PopUpResult = PopUpResult.NoResponse })
           );
   }
I feel ApplicationSpace maybe null for this..

crutkas wrote Dec 31, 2015 at 6:26 AM

I agree that is the root cause, but the question becomes why isn't there a dispatcher. Hence the need for a repro.

crutkas wrote Dec 31, 2015 at 6:26 AM

rg912 wrote Dec 31, 2015 at 8:29 AM

Can you put a null check and update lib?
This will at least prevent our app from crashing.

crutkas wrote Dec 31, 2015 at 9:05 PM

I'm 100% want to and willing to update, i just have a rule of any bug fixes need to have a way to test to be sure we don't regress and the actual issue is fixed. I think what could be happening is the app is getting suspended after the toast was kicked off.

Is this a phone app? Desktop app?

rg912 wrote Jan 1, 2016 at 12:59 PM