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

  • Question

  • User116164 posted

    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 BLEprototypev0002.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00021] in \BLEprototypev0002\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:

    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

    Tuesday, April 14, 2015 2:55 PM

All replies

  • User1341 posted

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

    Tuesday, April 14, 2015 4:32 PM
  • User209 posted

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

    Tuesday, April 14, 2015 4:34 PM
  • User1341 posted

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

    Tuesday, April 14, 2015 4:37 PM
  • User209 posted

    It is still wrong to not await it.

    Tuesday, April 14, 2015 4:42 PM
  • User1341 posted

    Thats true.

    Tuesday, April 14, 2015 4:44 PM
  • User116164 posted

    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 newnextrun) [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 BLEprototypedebug.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00021] in \BLEprototypedebug\BLEprototype_debug\MainActivity.cs:30

    Wednesday, April 15, 2015 5:04 AM
  • User78609 posted

    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

    Tuesday, April 21, 2015 3:01 PM
  • User78609 posted

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

    Tuesday, April 21, 2015 3:27 PM
  • User116164 posted

    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.

    Wednesday, April 22, 2015 3:55 PM
  • User116164 posted

    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.

    Monday, April 27, 2015 2:25 AM
  • User30998 posted

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

    Sunday, May 3, 2015 6:59 AM
  • User112 posted

    Guys, you should really report this to bugzilla.

    Sunday, May 3, 2015 9:08 AM
  • User25465 posted

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

    Thursday, August 6, 2015 6:49 PM
  • User209 posted

    @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.

    Thursday, August 6, 2015 8:27 PM
  • User361784 posted

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

    Friday, August 16, 2019 5:35 PM