I have used an if statement to check if the method retrieving the schema is null, this is in a separate form that contains the checkedListBox to populate. The code is below and I have marked the conditional that checks for this. My question is; What is the most effective method to ensure that each time the revit add-in is run in a new .rvt file, the schema record already exists before attempting to retrieve a schema? When things go awry a null reference error occurs when trying to access an empty schema.
//CheckedListBox for filter01 this exists in the form and calls the main
class function to retrieve the record.
checkedListBox1.DataSource = WS.categoryList(rvtDoc, intSwitch = 1);
Filter01_CategoryList = new List<BuiltInCategory>();
**if (WS.retSchemaBICMethod(rvtDoc) != null)**
{
TaskDialog.Show("Schema 1 ", " exists");
Filter01_CategoryList = WS.retSchemaBICMethod(rvtDoc);
}
else
{
TaskDialog.Show("Schema 1 "," has not been created");
//Update stored schema field values
inputBIC = checkedListBox1.CheckedItems.Cast<BuiltInCategory>
().ToList<BuiltInCategory>();
WS.getSetBIC(rvtDoc, inputBIC);
WS.storeSchema(rvtDoc, WS.projectInfoElement, inputBIC,
out WS.retrieveBIC);
//set checkedlistbox 1
Filter01_CategoryList = WS.retSchemaBICMethod(rvtDoc);
}
//this code returns the retrieved schema from the main class
public List<BuiltInCategory>retSchemaBICMethod(Document doc)
{
Element piElement = projectInfoFilter(doc);
// Read back the data from ProjInfo
Entity retrievedEntity = piElement.GetEntity(Schema.Lookup(schemaGuid));
IList<int> retrievedData = retrievedEntity.Get<IList<int>>
(Schema.Lookup(schemaGuid).GetField("BuiltInCatIds"));
//cast int list back to built-in category list
retSchemaBIC = retrievedData.Cast<BuiltInCategory>
().ToList<BuiltInCategory>();
return retSchemaBIC;
}
The GUID will most likely be different for each Revit project.
It is unclear how you are providing the guid to your retSchemaBICMethod, so it would seem that this is most likely the cause of your issue.
I would take the Schema.Lookup out of a GetEntity call and first check you have a valid value because I suspect that is where your null reference occurs.
I also believe there are issues with the way Revit handles multiple documents and extensible storage. I have had a number of issues occur where a second document is opened and have lodged a bug report with Revit on the issue.