locked
Passing Object to a Class RRS feed

  • Question

  • I hate to ask a simple thing like this but I've tried to read all the posts that I can find on the subject.

    I'm fairly new to C# programming and I'm stuck at how to pass an Object I've created to a class that needs to take care of the handling of the data.

    So, I'm populating my object with data and pass the object to my class safe.serializeObject.

                BaseSafeItem myItem = new BaseSafeItem();
                myItem.Title = itemTitle.Text;
                myItem.Content = itemContent.Text;
                myItem.IsProtected = (string.IsNullOrEmpty(itemPassword.Text)) ? false : true;
    
                string newItemSer = safe.serailizeObject(myItem);

    The class should the serialize the object and pass it back as a string, and this is not a problem if I do it inline.

           public string serailizeObject(object obj)
            {
                
                MemoryStream ms = new MemoryStream();
    
                DataContractJsonSerializer serItem = new DataContractJsonSerializer(typeof(BaseSafeItem));
                serItem.WriteObject(ms, obj);
                byte[] json = ms.ToArray();
    
                return Encoding.UTF8.GetString(json, 0, json.Length);
            }

     But the object can't be passed on and I get an error saying

    An exception of type 'System.NullReferenceException' occurred
    
    Additional information: Object reference not set to an instance of an object.

    How should I pass this object over to my class to handle the serialization of it?


    Friday, September 19, 2014 12:36 PM

Answers