locked
NullReference Exception in ReplicaMetadata class RRS feed

  • Question

  • Hi guys, I'm using the SQL metadata class to store the metadata for synchronization. I get Null pointer exception after I call GetChangeBatch from the ReplicaMetdata class. This siutation occurs only if I fill the ReplicaMetadata class with data. It does not occur on empty ReplicaMetdata. I've spend many hours trying to solve the problem but I cannot get to any reasonable explenation.

    Here is the simplified version of my code:

     

     

            public override void BeginSession(SyncProviderPosition position, SyncSessionContext syncSessionContext)       
            {           
                InitialzieMetadataStore();
                UpdateLocalMetadata();
            }


            public void InitialzieMetadataStore()
            {
                if (File.Exists(m_MetaDataStoreFileName))
                {               
                    m_MetadataStore = SqlMetadataStore.OpenStore(m_MetaDataStoreFileName);
                    m_ReplicaMetadata = m_MetadataStore.GetReplicaMetadata(m_IdFormat, m_ReplicaID);
                }
                else
                {
                    // Used to store external keys into metadata. The alternative is to use Object Identity Mapper
                    FieldSchema[] m_AdditionalMetadataSchema = null;
                    IndexSchema[] m_AdditionalMetadataIndex = null;

                    m_AdditionalMetadataSchema = new FieldSchema[3];
                    m_AdditionalMetadataSchema[0] = new FieldSchema("LocalTick", typeof(ulong), sizeof(ulong));
                    m_AdditionalMetadataSchema[1] = new FieldSchema("SystemName", typeof(string), 32);
                    m_AdditionalMetadataSchema[2] = new FieldSchema("LocalId", typeof(string), 32);

                    m_AdditionalMetadataIndex = new IndexSchema[2];
                    m_AdditionalMetadataIndex[0] = new IndexSchema("LocalTick", true);
                    m_AdditionalMetadataIndex[1] = new IndexSchema("LocalId", true);

                    m_MetadataStore = SqlMetadataStore.CreateStore(m_MetaDataStoreFileName);
                    m_ReplicaMetadata = m_MetadataStore.InitializeReplicaMetadata(m_IdFormat, m_ReplicaID, m_AdditionalMetadataSchema, m_AdditionalMetadataIndex);
                }
            }


            public override ChangeBatch GetChangeBatch(uint batchSize, SyncKnowledge destinationKnowledge, out object changeDataRetriever)
            {
                ChangeBatch batch = m_ReplicaMetadata.GetChangeBatch(batchSize, destinationKnowledge);

    /* NULL REFFERENCE EXCEPTION ! */
                changeDataRetriever = this;
                return batch;
            }

     

     public void UpdateLocalMetadata()
     {
                m_MetadataStore.BeginTransaction(global:Tongue Tiedystem.Data.IsolationLevel.ReadCommitted);
                try
                {
              /* some code the fetch data from external source */      
                   
                    while (...)
                    {
                       
                            currentTickCount = entityVersion.UpdateTimeStamp;
                            SyncId id = new SyncId(entityVersion.Key.ToString());
                            ItemMetadata found = m_ReplicaMetadata.FindItemMetadataById(id);
                            SyncVersion createVersion;
                           
                            createVersion = new SyncVersion(0, m_ReplicaMetadata.GetNextTickCount());
                           
                            if (found == null)
                                found = m_ReplicaMetadata.CreateItemMetadata(id, createVersion);
                            else
                                found.ChangeVersion = createVersion;

                            found.SetCustomField("SystemName", m_ReplicaMetadata.ReplicaId.GetStringId());
                            found.SetCustomField("LocalTick", currentTickCount);
                            found.SetCustomField("LocalId", entityVersion.Key.ToString());
                            m_ReplicaMetadata.SaveItemMetadata(found);
                    }
                        

                    m_ReplicaMetadata.CustomReplicaMetadata = /* some custom data, to mark the synchronization */

                    m_ReplicaMetadata.SaveReplicaMetadata();                                
                }
                catch (Exception e)
                {
                    m_MetadataStore.RollbackTransaction();
                    throw e;
                }           
                m_MetadataStore.CommitTransaction();
     }

    • Moved by Max Wang_1983 Thursday, April 21, 2011 9:41 PM forum consolidation (From:SyncFx - Technical Discussion [ReadOnly])
    Wednesday, September 10, 2008 10:52 AM

Answers

  • When I think back it's generally a minor bug, and it was my fault after all. But it took me so much time and pain to solve it just because the error was so general (Null pointer refrence). If I got a bit more specific error message, e.g. "No ChangeVersion was specified" there will be no problems at all. The solution is trivial, but only when you know the diagnosys.  Well this is just my thinking ...

     

    Here is the stack trace:

     

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

    "   at Microsoft.Synchronization.MetadataStorage.IReplicaMetadata.GetChangeBatch(UInt32 dwBatchSize, ISyncKnowledge pSyncKnowledge, ISyncChangeBatch& ppSyncChangeBatch)\r\n   at Microsoft.Synchronization.MetadataStorage.SqlReplicaMetadata.GetChangeBatch(UInt32 batchSize, SyncKnowledge destinationKnowledge)\r\n   at Revolution.Assets.System.Integration.Sync.EntitySyncProvider.GetChangeBatch(UInt32 batchSize, SyncKnowledge destinationKnowledge, Object& changeDataRetriever) in C:\\My App\\PlatformV1\\Dev\\Assets\\Sys\\Int\\Sync\\EntitySync\\EntitySyncProvider\\EntitySyncProvider.cs:line 339\r\n   at Microsoft.Synchronization.KnowledgeProviderProxy.GetChangeBatch(UInt32 dwBatchSize, ISyncKnowledge pSyncKnowledge, ISyncChangeBatch& ppChangeBatch, Object& ppUnkDataRetriever)\r\n   at Microsoft.Synchronization.CoreInterop.ISyncSession.Start(CONFLICT_RESOLUTION_POLICY resolutionPolicy, _SYNC_SESSION_STATISTICS& pSyncSessionStatistics)\r\n   at Microsoft.Synchronization.KnowledgeSyncOrchestrator.DoOneWaySyncHelper(SyncIdFormatGroup sourceIdFormats, SyncIdFormatGroup destinationIdFormats, KnowledgeSyncProviderConfiguration destinationConfiguration, SyncCallbacks DestinationCallbacks, ISyncProvider sourceProxy, ISyncProvider destinationProxy, Int32& changesApplied, Int32& changesFailed)\r\n   at Microsoft.Synchronization.KnowledgeSyncOrchestrator.DoOneWayKnowledgeSync(SyncProvider sourceProvider, SyncProvider destinationProvider, Int32& changesApplied, Int32& changesFailed)\r\n   at Microsoft.Synchronization.KnowledgeSyncOrchestrator.Synchronize()\r\n   at Microsoft.Synchronization.SyncOrchestrator.Synchronize()\r\n   at EntitySync.Test.EntitySyncProviderTest.EntitySyncWithDummyProvider_DownloadAndUpload_Test() in C:\\My App\\PlatformV1\\Dev\\Assets\\Sys\\Int\\Sync\\EntitySync\\Test\\Automated Tests\\EntitySyncProviderTest.cs:line 321"

     

    Thank for the answer,

    Bogdan

     

     

    Friday, September 12, 2008 7:32 AM

All replies

  • I think I found the error. When you create a new version you must SET the change version.

     

    found = m_ReplicaMetadata.CreateItemMetadata(id, createVersion);
    found.ChangeVersion = createVersion;

     

    I'm not sure but it looks like this one is a minor bug from Microsoft. CreateItemMetadata should have set the change version for me.

    It would be nice if the exception is bit more specialzed the NullReference. I lost a lot of time guessing what is wrong.

     

    Bogdan

    Wednesday, September 10, 2008 11:53 AM
  • I'm glad you found the problem.  Can you also post the stack trace of the exception?  If it comes from the metadata store code we should look at it.

     

    Thanks!

    Michael Clark

     

    Friday, September 12, 2008 4:09 AM
    Moderator
  • When I think back it's generally a minor bug, and it was my fault after all. But it took me so much time and pain to solve it just because the error was so general (Null pointer refrence). If I got a bit more specific error message, e.g. "No ChangeVersion was specified" there will be no problems at all. The solution is trivial, but only when you know the diagnosys.  Well this is just my thinking ...

     

    Here is the stack trace:

     

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

    "   at Microsoft.Synchronization.MetadataStorage.IReplicaMetadata.GetChangeBatch(UInt32 dwBatchSize, ISyncKnowledge pSyncKnowledge, ISyncChangeBatch& ppSyncChangeBatch)\r\n   at Microsoft.Synchronization.MetadataStorage.SqlReplicaMetadata.GetChangeBatch(UInt32 batchSize, SyncKnowledge destinationKnowledge)\r\n   at Revolution.Assets.System.Integration.Sync.EntitySyncProvider.GetChangeBatch(UInt32 batchSize, SyncKnowledge destinationKnowledge, Object& changeDataRetriever) in C:\\My App\\PlatformV1\\Dev\\Assets\\Sys\\Int\\Sync\\EntitySync\\EntitySyncProvider\\EntitySyncProvider.cs:line 339\r\n   at Microsoft.Synchronization.KnowledgeProviderProxy.GetChangeBatch(UInt32 dwBatchSize, ISyncKnowledge pSyncKnowledge, ISyncChangeBatch& ppChangeBatch, Object& ppUnkDataRetriever)\r\n   at Microsoft.Synchronization.CoreInterop.ISyncSession.Start(CONFLICT_RESOLUTION_POLICY resolutionPolicy, _SYNC_SESSION_STATISTICS& pSyncSessionStatistics)\r\n   at Microsoft.Synchronization.KnowledgeSyncOrchestrator.DoOneWaySyncHelper(SyncIdFormatGroup sourceIdFormats, SyncIdFormatGroup destinationIdFormats, KnowledgeSyncProviderConfiguration destinationConfiguration, SyncCallbacks DestinationCallbacks, ISyncProvider sourceProxy, ISyncProvider destinationProxy, Int32& changesApplied, Int32& changesFailed)\r\n   at Microsoft.Synchronization.KnowledgeSyncOrchestrator.DoOneWayKnowledgeSync(SyncProvider sourceProvider, SyncProvider destinationProvider, Int32& changesApplied, Int32& changesFailed)\r\n   at Microsoft.Synchronization.KnowledgeSyncOrchestrator.Synchronize()\r\n   at Microsoft.Synchronization.SyncOrchestrator.Synchronize()\r\n   at EntitySync.Test.EntitySyncProviderTest.EntitySyncWithDummyProvider_DownloadAndUpload_Test() in C:\\My App\\PlatformV1\\Dev\\Assets\\Sys\\Int\\Sync\\EntitySync\\Test\\Automated Tests\\EntitySyncProviderTest.cs:line 321"

     

    Thank for the answer,

    Bogdan

     

     

    Friday, September 12, 2008 7:32 AM
  • Hello Bogdan,

     

    Bug has been filed to improve the error messaging.

     

    Thank you for your valuable feedback.

     

    Patrick

    Friday, September 26, 2008 5:20 PM