Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
695
Error populating datasourcehelper with data
posted

I have attached a project that demonstrates the problem.

What I am attempting to do is to serialize some information and save it to a text file. When my application launches, I will read the file, deserialize the string(s) back in to objects and use these objects to populate my datasource helper. This will then repopulate my grid.

The problem occurs when trying to add the list of deserialized objects back to the datasourcehelper. I have pasted a stack trace below.

MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: *** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]
  at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSend_IntPtr_int (intptr,intptr,intptr,int)
  at MonoTouch.Foundation.NSArray.FromObjects (IntPtr array, Int32 count) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/Foundation/NSArray.g.cs:108
  at MonoTouch.Foundation.NSArray.FromNSObjects (IList`1 items) [0x0004f] in /Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSArray.cs:83
  at MonoTouch.Foundation.NSArray.FromNSObjects (MonoTouch.Foundation.NSObject[] items) [0x00002] in /Developer/MonoTouch/Source/monotouch/src/shared/Foundation/NSArray.cs:49
  at Infragistics.IGGridViewDataSourceHelper.set_Data (MonoTouch.Foundation.NSObject[] value) [0x00000] in <filename unknown>:0
  at jsontestapp.jsontestappViewController.ViewDidLoad () [0x0003c] in /Users/pauldavey/Projects/jsontestapp/jsontestapp/jsontestappViewController.cs:45
  at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr)
  at MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00010] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIWindow.g.cs:128
  at jsontestapp.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00031] in /Users/pauldavey/Projects/jsontestapp/jsontestapp/AppDelegate.cs:31
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at jsontestapp.Application.Main (System.String[] args) [0x00000] in /Users/pauldavey/Projects/jsontestapp/jsontestapp/Main.cs:16

jsontestapp.zip
Parents
  • 40030
    Offline posted

    Hi Paul, 

    The exception is actually getting thrown by the Xamarin.ios framework when it tries to cover the NSObject[] to an array. The reason for the exception, is b/c of how the Json serializer/deserializer work. 

    If you look at the seraized json string, you'll noted the following:

    {"name":"bob","age":"29","Handle":{},"SuperHandle":{},"RetainCount":1,"ClassHandle":{}}

    Now, all you wanted to serialize was name and age. SuperHandle and ClassHandle are both read only, so nothing will happen there. However, Handle IS settable. And so after it creates the object it sets it's Handle to nil. Now thats a problem, b/c when you try to go back to objective-c as it's doing when you set the DataSourceHelper's data property, it doesn't have a handle on the objective-c version of the object, so it thinks it's nil. And thus the exception. 

    I don't know if there is a better solution, but i did find one. You can create your own' version of Handle and filter on nil:

    public class Person : NSObject
        {
        
            [Export("name")]
            public string name { get; set; }
            [Export("age")]
            public string age { get; set; }

            public IntPtr Handle
            { 
                get
                {
                    return base.Handle;
                }
                set 
                {
                     if (value != IntPtr.Zero)
                        base.Handle = value;
                }

            }
        }

    Hope this helps,

    -SteveZ

Reply Children
No Data