Even creating a new NSObject will cause a NullPointerException

I am trying to access a OS X driver through a framework that I've written in MonoMac. In that process I thought it would be easier if I would stick to the Simple tutorial and make that work first before adding more complexity like dependencies on other dylibs. With some perseverance I managed to get Simple to Write "OK" to the console and I started to work to convert the driver Framework following the now familiar path I had followed by converting Simple.Framework to Simple.dll.

However this time whenever I was trying to create an instance of any class inside that Driver.Framework it would throw a NullPointerException. So I tried quite a few things until I started to narrow down what the real problem was by cutting off parts of that framework to see where the problem was. Even just using stubs it would crash. Then I wondered if my Simple.dll would still work and even that would crash in the same way. Then I created a new Test project and Simple.dll still didn't work. Then I didn't even include Simple.dll and just added the following line of code:

var test = new NSObject();

This should work, shouldn't it? But exactly same runtime error occurred:

System.NullReferenceException: Object reference not set to an instance of an object at MonoMac.ObjCRuntime.Class.Register (System.Type type) [0x00007] in /Users/builder/data/lanes/1248/f2847d5f/source/xamcore/maccore/src/ObjCRuntime/Class.cs:134 at MonoMac.ObjCRuntime.Class.GetHandle (System.Type type) [0x00002] in /Users/builder/data/lanes/1248/f2847d5f/source/xamcore/maccore/src/ObjCRuntime/Class.cs:103 at MonoMac.Foundation.NSObject.AllocIfNeeded () [0x0001e] in /Users/builder/data/lanes/1248/f2847d5f/source/xamcore/maccore/src/Foundation/NSObject2.cs:475 at MonoMac.Foundation.NSObject..ctor () [0x00008] in /Users/builder/data/lanes/1248/f2847d5f/source/xamcore/maccore/src/Foundation/NSObject2.cs:106 at Test2.MainClass.Main (System.String[] args) [0x00001] in /Users/Lucas/Code/Xamarin/Test2/Test2/Main.cs:16

So I am wondering if there might be some kind of mixup happening inside Xamarin because I first installed MacMono, then hand compiled MacMono to obtain bmac.exe to only upgrade in the end to Xamarin.Mac. Then I installed one or two upgrades when prompted, now running 5.8.3.

Who can shed some light on this?

Posts

  • RolfBjarneKvingeRolfBjarneKvinge USXamarin Team Xamurai

    Have you tried creating a new Xamarin.Mac project and see if the same thing happens there?

    If that works you can compare the two projects and try to find out what's different between them.

  • LucasvanDongenLucasvanDongen ARMember

    I tried creating two test projects. One with Xamarin.Mac, one with MonoMac. So Simple.dll only works with Xamarin.Mac, not with MonoMac, probably because I used bmac from Xamarin.Mac because I was getting really frustrated with bmac.exe that I manually compiled from source.

    The project that crashed probably ended up being a conflicting mix in some sort of way because I created that before installing the trial version of Xamarin.Mac. So it's not possible to use dll's compiled with bmac in MonoMac and vice versa? The original framework contained a Hello World method, nothing exotic that could warrant bmac.exe to fail.

  • RolfBjarneKvingeRolfBjarneKvinge USXamarin Team Xamurai

    No, it's not possible to mix dlls compiled with MonoMac and Xamarin.Mac.

  • LucasvanDongenLucasvanDongen ARMember
    edited May 2015

    I've updated this morning and ever since then I cannot instantiate native classes directly from C#, including NSObject. I've created a new Xamarin Classic OS X project that only instantiates an NSObject in main.cs and it crashes already.

    using System;
    using System.Drawing;
    using MonoMac.Foundation;
    using MonoMac.AppKit;
    using MonoMac.ObjCRuntime;
    
    namespace MSN_OS_X
    {
        class MainClass
        {
            static void Main (string[] args)
            {
                if (Dlfcn.dlopen("/Users/Lucas/Projects/TempTest/TempTest/OPNScannerFramework.framework/OPNScannerFramework", 0) == IntPtr.Zero) 
                {
                    Console.Error.WriteLine("Error: " + Dlfcn.dlerror());
                }
    
                var test = new NSObject (); // NullPointerException here
                //OptiScannerFactory.RegisteredScanner = new ScannerImplementation ();
    
                NSApplication.Init ();
                NSApplication.Main (args);
            }
        }
    }
    

    When I create a new project for the Unified API it just works.

  • LucasvanDongenLucasvanDongen ARMember

    I re-installed the trial version mentioned here, cleaned the project and it all started working again. I've installed updates before so I guess the last update was a 2.0 update and that one doesn't work so well at the moment.

  • RolfBjarneKvingeRolfBjarneKvinge USXamarin Team Xamurai

    You must call NSApplication.Init () before you can create NSObjects.

  • LucasvanDongenLucasvanDongen ARMember

    This worked at least for the commercial version of Xamarin.Mac. If I find the time I will see if it fixes the same problem I had with MonoMac but my client decided that App Store supported was needed so I'll stick to Xamarin.Mac anyway.

Sign In or Register to comment.