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;
}