Task.delay(): System.NullReferenceException: Object reference not set to an instance of an object

Hi,

Thank you for looking into this problem!
I have been getting the exception "System.NullReferenceException: Object reference not set to an instance of an object" when trying to invoke "task.delay()" right after SetContentView() in OnCreate().

Code:
public class MainActivity : Activity, ILocationListener
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        Console.WriteLine ("Starting the delay task!");
        try {
            Task.Delay (2000);
        }
        catch(Exception e) {
            Console.WriteLine ("Error occured at Task.delay(): {0}", e);
        }

Application Output and Exception Message:
[chromium] ### WebView Version 40 (1832189-arm) (code 424501)
Thread started: #2
Error occured at Task.delay(): System.NullReferenceException: Object reference not set to an instance of an object
at System.Threading.Timer..ctor (System.Threading.TimerCallback callback, System.Object state, Int32 dueTime, Int32 period) [0x00000] in :0
at (wrapper remoting-invoke-with-check) System.Threading.Timer:.ctor (System.Threading.TimerCallback,object,int,int)
at System.Threading.Tasks.Task.Delay (Int32 millisecondsDelay, CancellationToken cancellationToken) [0x00000] in :0
at System.Threading.Tasks.Task.Delay (Int32 millisecondsDelay) [0x00000] in :0
at BLEprototype_v0002.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00021] in \BLEprototype_v0002\BLEprototype_v0002\MainActivity.cs:33

More Interestingly, if I invoke the "Task.Delay()" before "setContentView()", there would be no exception.
The Resource.Layout.Main is a webView:
<?xml version="1.0" encoding="utf-8"?>

Target: Nexus 5, Android 5.1
Xamarin Studio: 5.8.3

I'd be really appreciate for any hint on how this error occur...

Thank you,
Peter

Answers

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    I can not reproduce this behavior within an app of mine. Can you provide a small sample?

  • CheesebaronCheesebaron DKInsider, University mod

    You are not awaiting the Task.Delay call, the code will just continue without waiting...

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Not awaiting Task.Delay shouldn't throw an exception. There must be something else wrong.

  • CheesebaronCheesebaron DKInsider, University mod

    It is still wrong to not await it.

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Thats true.

  • PeterHsPeterHs CAMember

    Thank you both for you replies!
    It is indeed a weird bug (also my lack of my experience in C# and the Xamarin world..)

    I have tried both ways of implementation, with and without the await.
    (I guess what I really want to find out is why Task.Delay() would result in such error.)

    The MainActivity.txt (actually a .cs) is attached, it is also the only code that is running.
    It seems like it has something to do with the use of "webview" because when I tried such implementation on other sample apps say Tasky which don't require the use of webview, such error doesn't occur.

    Code: (without await)
    [Activity (Label = "Debug", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
    protected WebView WebView;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
    
            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
    
            Console.WriteLine ("Starting the delay task!");
            try {
                Task.Delay (5000);
            }
            catch(Exception e) {
                Console.WriteLine ("Error occured at Task.delay(): {0}", e);
            }
    
            WebView = FindViewById<WebView>(Resource.Id.WebView);
            WebView.Settings.JavaScriptEnabled = true;
            WebView.LoadUrl("http://www.google.com");
        }
    }
    

    The await version is in the attached file.
    (I am not completely sure I did it right, but it was compiled and ran on my target)

    Application output and exception message (same / very similar to the one in the original post):
    Starting the delay task!
    [chromium] ### WebView Version 40 (1832189-arm) (code 424501)
    Thread started: #2
    Error occured at Task.delay(): System.NullReferenceException: Object reference not set to an instance of an object
    at System.Collections.SortedList.EnsureCapacity (Int32 n, Int32 free) [0x00000] in :0
    at System.Collections.SortedList.PutImpl (System.Object key, System.Object value, Boolean overwrite) [0x00000] in :0
    at System.Collections.SortedList.Add (System.Object key, System.Object value) [0x00000] in :0
    at System.Threading.Timer+Scheduler.Add (System.Threading.Timer timer) [0x00000] in :0
    at System.Threading.Timer+Scheduler.Change (System.Threading.Timer timer, Int64 new_next_run) [0x00000] in :0
    at System.Threading.Timer.Change (Int64 dueTime, Int64 period, Boolean first) [0x00000] in :0
    at System.Threading.Timer.Init (System.Threading.TimerCallback callback, System.Object state, Int64 dueTime, Int64 period) [0x00000] in :0
    at System.Threading.Timer..ctor (System.Threading.TimerCallback callback, System.Object state, Int32 dueTime, Int32 period) [0x00000] in :0
    at (wrapper remoting-invoke-with-check) System.Threading.Timer:.ctor (System.Threading.TimerCallback,object,int,int)
    at System.Threading.Tasks.Task.Delay (Int32 millisecondsDelay, CancellationToken cancellationToken) [0x00000] in :0
    at System.Threading.Tasks.Task.Delay (Int32 millisecondsDelay) [0x00000] in :0
    at BLEprototype_debug.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00021] in \BLEprototype_debug\BLEprototype_debug\MainActivity.cs:30

  • Sam.3816Sam.3816 USMember ✭✭

    Hi There this seems to be very similar to the issue I am experiencing when debugging a webview application under lollipop, I have no resolution but is your isseu only happening under debug and under a lollipop device ?

    I ask as I am seeing

    [chromium] ### WebView Version 40 (1832189-arm) (code 424501)

    in my console when my app fails

  • Sam.3816Sam.3816 USMember ✭✭

    Reading around on this error it appears to be very widespread hopefully someone will offer a resolution soon.

  • PeterHsPeterHs CAMember

    Hi Sam,

    Good thing you brought it up.
    I tried running the app in release instead of debug, the task.delay() worked fine! - no more exceptions.
    However, as I changed it back to debug, the problem is still there.
    As of the Android version, I am running 5.1 indeed.

    I actually didn't need the task.delay() at where it was in the code I provided above.
    Originally, I encountered this task.delay() exception from an API within a 3rd party package that I use. (Monkey.Robotics)
    Before using webview, I used fragments to develop the UI along with the Monkey.Robotics package, where calling the same API (StartScanningForDevices()) wouldn't throw the task.delay() exception.
    Therefore, I suspected this bug could be related to the use of webview. So I stripped out everything, leaving just the sample code you can see from my previous post and yes, the task.delay() exception still happens.

    One interesting thing tho,
    ever since I put the task.delay() there after setcontentview(), despite the first exception which is caught, any subsequent calls to task.delay() would work fine without exception thrown.
    So, it became a workaround for me now... a very nasty hack.

  • PeterHsPeterHs CAMember

    By the way,
    This happened on a Nexus 5.
    We tried another phone today, an Asus Zen phone running Android 4.4.2,
    such error didn't occur.

  • WilliamJockuschWilliamJockusch USMember ✭✭

    Same problem here, on a Google Nexus 7. Crashes in debug more with a null reference exception, works fine in release mode.

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    Guys, you should really report this to bugzilla.

  • LennartBergLennartBerg USMember

    Having the same issue on Mono 4.0 with debian linux ( rasberry PI 2)
    has this been reported?

  • CheesebaronCheesebaron DKInsider, University mod

    @LennartBerg I don't think so. I haven't seen the issue in any of my apps. Please go ahead and report it on bugzilla.

  • Esaavedra89Esaavedra89 USMember ✭✭

    I'm getting this exception using await Task.Run() but the error occur even in release mode.

Sign In or Register to comment.