Bug 12901 - Error in Get_Application on Mac but not PC (4.7.9 Beta)
Summary: Error in Get_Application on Mac but not PC (4.7.9 Beta)
Status: RESOLVED DUPLICATE of bug 12544
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.7.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-06-26 11:40 UTC by Neal
Modified: 2013-06-26 14:24 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
error info (386.35 KB, image/png)
2013-06-26 11:40 UTC, Neal
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED DUPLICATE of bug 12544

Description Neal 2013-06-26 11:40:37 UTC
Created attachment 4207 [details]
error info

Hello,

I'm using the following singleton pattern to get access to the Application...

namespace LogbookPro
{
    [Application]
    public class LogbookProApplication : Application
    {
        protected LogbookProApplication(IntPtr javaReference, JniHandleOwnership transfer)
            : base(javaReference, transfer)
        {}
        public LogbookProApplication()
        {}
        public override void OnCreate()
        {
            base.OnCreate();
        }
....

I'm getting the following exception on Mac, but NOT in Windows/Visual Studio 2012. I'm on the beta channel on both Mac and PC (4.7.9)

System.Diagnostics.Debugger.Mono_UnhandledException (ex={System.NullReferenceException: Object reference not set to an instance of an object
at Java.Lang.Object.get_Handle () [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.7.9-branch/47cd3984/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:68
at Android.App.Activity.get_Application () [0x0002d] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.7.9-branch/47cd3984/source/monodroid/src/Mono.Android/platforms/android-17/src/generated/Android.App.Activity.cs:144
at LogbookPro.BaseActivity.get_App () [0x00001] in /Users/Neal/Dev/LogbookPro/Android/Activities/Base/BaseActivity.cs:42
at LogbookPro.BaseActivity/<OnCreateDialog>c__AnonStorey4.<>m__F (object,System.EventArgs) [0x00024] in /Users/Neal/Dev/LogbookPro/Android/Activities/Base/BaseActivity.cs:245
at Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) [0x0000b] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.7.9-branch/47cd3984/source/monodroid/src/Mono.Android/platforms/android-17/src/generated/Android.Views.View.cs:1780
at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.7.9-branch/47cd3984/source/monodroid/src/Mono.Android/platforms/android-17/src/generated/Android.Views.View.cs:1751
at (wrapper dynamic-method) object.21dd8752-ba9a-425f-a870-9a406470e83f (intptr,intptr,intptr) <IL 0x00017, 0x0001f>
}) in
Comment 1 Jonathan Pryor 2013-06-26 12:02:34 UTC
Please provide a complete reproducible test case. There's nothing in the `LogbookProApplication` that should be a problem (though the default construtor isn't needed).
Comment 6 Jonathan Pryor 2013-06-26 13:48:02 UTC
I believe that this is a mcs C# compiler bug; BaseActivity.OnCreateDialog() is (simplified):

        protected override Dialog OnCreateDialog(int id)
        {
            switch (id)
            {
                case 1:
                    var dialog = new Dialog(this);
                    var save = dialog.FindViewById<Button>(Resource.Id.sal_button_ok);
                    save.Click += delegate
                    {
                        this.App.Settings.Foo = 42;

The problem is that within the Button.Click event handler, `this` is in fact null, so the NullReferenceException is "correct", if absolutely bizarre and wrong

_However_, if we explicitly capture `this`, it works, and the NullReferenceException is no longer generated:

        protected override Dialog OnCreateDialog(int id)
        {
            switch (id)
            {
                case 1:
                    var dialog = new Dialog(this);
                    var save = dialog.FindViewById<Button>(Resource.Id.sal_button_ok);
                    var self = this;
                    save.Click += delegate
                    {
                        self.App.Settings.Foo = 42;

That shouldn't make a difference, but it works around this particular problem.

I'll try to create a simpler repro for the compiler guy.
Comment 7 Neal 2013-06-26 13:54:34 UTC
Thanks Jon, the odd part is it works in VS (PC) but not XS (Mac).  I'm trying to determine which is actually better for development and was leaning towards Mac as the emulator seems more responsive and builds are quicker but this one sent me back to VS.  

There's another possible bug, if you need me to open a new case let me know.  If you look at my project in VS the supported architectures are all checked.  The same project on Mac, NONE are checked.  Some other things are different between the two as well such as Install location, Mac has "auto" and VS does not, it has prefer internal, etc.  I'm just trying to decide again which is really better and more accurate as they are not clones of each other between PC and Mac.

Thanks

P.S.  My office is in Richmond, VA - I think you're somewhere out this way :)
Comment 8 Jonathan Pryor 2013-06-26 14:02:56 UTC
> the odd part is it works in VS (PC) but not XS (Mac)

That's exactly why it's a compiler bug. ;-)

> If you look at my project in VS the supported architectures are all checked. 
> The same project on Mac, NONE are checked.

You mean Supported ABIs, in Project Options > Build / Android Build > Advanced tab > Supported ABIs? They're all checked for me for Debug builds, but not for Release builds.

Looking at the .csproj I see why:

    <!-- global options -->
    <AndroidSupportedAbis>armeabi%3barmeabi-v7a%3bx86</AndroidSupportedAbis>

    <!-- Debug options -->
    <AndroidSupportedAbis>armeabi,armeabi-v7a,x86</AndroidSupportedAbis>

For some reason in the global section it's using %3b instead of ',' or ';'. I wonder which IDE did that... :-/

I would suggest manually editing the .csproj to replace %3b with ',' (comma). That cause Xamarin Studio to display things appropriately for all build configurations.

> My office is in Richmond, VA

Whereabouts? I'm in the West End, near Parham & Broad.
Comment 9 Neal 2013-06-26 14:12:36 UTC
Thanks Jon, I'll make the edit.  My guess is VS made that entry in the .csproj as I used VS solely in the past and my earliest SVN build has it in there.

Do you have a nudge as to which is better for Android dev?  PC or Mac?  

I'm in the 8th & Main building downtown (at 8th and Main :)).  I grew up in the west end, had an office at Innsbrook, then south side in Moorefield park, now downtown as of a few months.
Comment 10 Jonathan Pryor 2013-06-26 14:19:48 UTC
After consultation with the compiler bug, this appears to be Bug #12544.

Simplified test case: 

  using System;

  class Test {
    public static void Main ()
    {
      var v = new Test ().Run (42);
      v.RaiseClick ();
    }

    public int Value;

    public View Run (int value)
    {
      switch (value) {
        case 42:
          var v = new View ();
          v.Click += delegate {
            v.Extra = 42;
            this.Value = 42;
          };
          return v;
        default: return null;
      }
    }
  }

  class View {
    public event EventHandler Click;

    public int Extra;

    public void RaiseClick ()
    {
      var h = Click;
      if (h != null)
        h (this, EventArgs.Empty);
    }
  }


Execution results in the NullReferenceException:

> System.NullReferenceException: Object reference not set to an instance of an object
>   at Test+<Run>c__AnonStorey0.<>m__0 (System.Object , System.EventArgs ) [0x0000e] in /Users/jon/tmp/csharp/bxc12901.cs:19 
>   at View.RaiseClick () [0x0000e] in /Users/jon/tmp/csharp/bxc12901.cs:36 
>   at Test.Main () [0x0000e] in /Users/jon/tmp/csharp/bxc12901.cs:7

*** This bug has been marked as a duplicate of bug 12544 ***
Comment 11 Jonathan Pryor 2013-06-26 14:21:36 UTC
> Do you have a nudge as to which is better for Android dev?  PC or Mac?  

This particular bug aside, I do most of my development on Mac.

Then again, I also live inside Terminal.app and MacVim.app, so I'm not exactly "normal"...
Comment 12 Neal 2013-06-26 14:24:23 UTC
LOL - okay, thanks!  I'll rotate 90 right and give Mac a try again :)