Message 1 of 6
Object reference not set to an instance of an object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Almost three weeks ago I have a problem that I could not solve. I have searched and found many options but none has worked for me. The error when I try to count the elements that have the shared parameter "elemento" equal. I get the following error "Object reference not set to an instance of an object". My code is not very complex. I have tried to make conditional, but I do not find good results. I would be very grateful for any recommendations.
UIApplication uiApp = commandData.Application; UIDocument uiDoc = uiApp.ActiveUIDocument; Autodesk.Revit.ApplicationServices.Application app = uiApp.Application; Document doc = uiDoc.Document; lstElement = new List<Element>(); lstElement = Util.AllElements(doc); List<string> lst = new List<string>(); foreach (Element e in lstElement) { if (e.LookupParameter("Elemento") != null) { lst.Add(Util.ParameterToString(e.LookupParameter("Elemento"))); } } var query = from item in lst let palabra = item.ToString() group item by palabra into g select new { Key = g.Key, Values = g }; string nRepetidas = ""; foreach (var item in query) { nRepetidas += string.Format("{0} = {1}" + "\n", item.Key, item.Values.Count()); } TaskDialog Ventana = new TaskDialog("Cantidad de Elementos"); Ventana.MainInstruction = "El proyecto tiene " + lstElement.Count.ToString(); Ventana.MainContent = "Se han modelado los siguientes elementos :" + "\n" + "\n" + nRepetidas; Ventana.Show(); return Result.Succeeded;
The "Util" methods I use are the following
public static List<Element> AllElements (Document doc) { List<Element> lstElement = new List<Element>(); FilteredElementCollector Columnas = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)) .OfCategory(BuiltInCategory.OST_StructuralColumns); List<Element> lstColumnas = Columnas.ToList(); FilteredElementCollector Vigas = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)) .OfCategory(BuiltInCategory.OST_StructuralFraming); List<Element> lstVigas = Vigas.ToList(); FilteredElementCollector Escaleras = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)) .OfCategory(BuiltInCategory.OST_Stairs); List<Element> lstEscaleras = Escaleras.ToList(); List<Element> lstWall = FilterElementFamilyType(doc, typeof(Wall)); List<Element> lstFloor = FilterElementFamilyType(doc, typeof(Floor)); foreach (Element e in lstColumnas) { lstElement.Add(e); } foreach (Element e in lstVigas) { lstElement.Add(e); } foreach (Element e in lstFloor) { lstElement.Add(e); } foreach (Element e in lstWall) { lstElement.Add(e); } foreach (Element e in lstEscaleras) { lstElement.Add(e); } return lstElement; }
public static string ParameterToString(Parameter param) { string val = "none"; if (param == null) { return val; } switch (param.StorageType) { case StorageType.Double: double dval = param.AsDouble(); val = dval.ToString(); break; case StorageType.Integer: int iVal = param.AsInteger(); val = iVal.ToString(); break; case StorageType.String: string sVal = param.AsString(); val = sVal; break; case StorageType.ElementId: ElementId idVal = param.AsElementId(); val = idVal.IntegerValue.ToString(); break; case StorageType.None: break; default: break; } return val; }
Regards,