C# – Using RdfProperty of array to custom class

cnetrdfrowlexsemweb

I have defined:

[RdfSerializable]
public class SomeItem
{
   // Unique identificator of the resource
   [ResourceUri]
   public string ID { get; set; }

   [RdfProperty( true )]
   public string SomeData { get; set; }
}

and in some other class: 

[RdfProperty(true)]
public SomeItem[] MyTestProp
{
   get
   {
      return new SomeItem[] { new SomeItem() { ID="1", SomeData="test1" }, new SomeItem() { ID="2", SomeData = "test2" } };
   }
}

When I try to serialize the class that contains this custom "MyTestProp" and it gave me that message:

Object reference not set to an
instance of an object.

Description: An unhandled exception
occurred during the execution of the
current web request. Please review the
stack trace for more information about
the error and where it originated in
the code.

Exception Details:
System.NullReferenceException: Object
reference not set to an instance of an
object.

Am I wrong in defining those properties or there is a special way to define array to custom class? Note that serializing array to string for example is not crashing me that way, but it is working.

Whole source:

using System;
using NC3A.SI.Rowlex;

[assembly: Ontology("ROWLEXtest1", "http://www.test.com/MyOntology")]

namespace ROWLEXtest1
{
   [RdfSerializable( HasResourceUri=false )]
   public class Item
   {
      [RdfProperty(true)]
      public string MyProp;
   }

   [RdfSerializable]
   public class AllItems
   {
      [RdfProperty(true)] public string mTitle;

      private int id = new Random().Next(0, 20);

      [ResourceUri]
      public string ResourceUri 
      {
         get { return "This " + id.ToString(); }
      }

      [RdfProperty(false)]
      public Item[] Items;
   }

   class Program
   {
      static void Main(string[] args)
      {
         var item = new AllItems();
         item.mTitle = "Hello World!";
         item.Items = new Item[] { new Item(){ MyProp = "test1" }, new Item(){ MyProp = "test2" } };

         var doc = Rdfizer.Serialize(item);

         System.Console.Out.Write(doc.ToString());
      }
   }
}

Exception is:

System.NullReferenceException was
unhandled Message="Object reference
not set to an instance of an object."
Source="NC3A.SI.Rowlex" StackTrace:
at NC3A.SI.Rowlex.RdfPropertyAttribute.ExtractRange(MemberInfo
memberInfo, Int32& minCardinality,
Int32& maxCardinality)
at NC3A.SI.Rowlex.RdfPropertyAttribute.ExtractRange(MemberInfo
memberInfo)
at NC3A.SI.Rowlex.Rdfizer.AppendProperty(RdfDocument
doc, MemberInfo memberInfo,
RdfPropertyAttribute attribute, Object
item, String resourceUri)
at NC3A.SI.Rowlex.Rdfizer.AppendSingleRdfSerializableObject(RdfDocument
doc, Object item)
at NC3A.SI.Rowlex.Rdfizer.ProcessItem(RdfDocument
doc, Object item, String[]
rangeTypeUris)
at NC3A.SI.Rowlex.Rdfizer.ExecuteSerialization(IEnumerable
objects)
at NC3A.SI.Rowlex.Rdfizer.Serialize(IEnumerable
objects, Boolean
tolerateUnserializebleObjects)
at NC3A.SI.Rowlex.Rdfizer.Serialize(Object
item)
at ROWLEXtest1.Program.Main(String[]
args) in
C:\ROWLEXtest1\ROWLEXtest1\Program.cs:line
40
at System.AppDomain._nExecuteAssembly(Assembly
assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback
callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Best Answer

What you did looks OK but there is one error: RdfProperty declaration should take "false" for MyTestProp as MyTestProp is not a data type property but object property (it returns objects and not literals).

However, I am not sure that this is the root of your problem. And even if it is, you should get a decent error message with meaningful text instead of silly NullReferenceException. Therefore, I would like to try to reproduce your error and provide a fix if applicable. Could you please, specify

  • the class and its decoration that hosts the MyTestProp,
  • the code where you instantiate that class, and
  • the code you use to for serialization.
  • Had you apply assembly level attributes (for ontology - namespace mapping), please indicate that, too.

Maybe you could consider sending me your code sample to [admin at rowlex.net].

EDIT: I could reproduce the exception, it is a bug in ROWLEX. The fixed 2.0.1 version can now downloaded from the ROWLEX site.