system.nullreference exception object reference

system.nullreference exception object reference

T52K-BIB
Enthusiast Enthusiast
1,468 Views
2 Replies
Message 1 of 3

system.nullreference exception object reference

T52K-BIB
Enthusiast
Enthusiast

hi, I'm New to Revit API and learning C# recently and below error comes to whileRun Program.

Anyone can help me.

 

"system.nullreferenceexception object reference"

 

 

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace COBie_Door_Space
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;
            
 
             // Use OfCategory method to apply an ElementCategoryFilter and fine elements in the Doors category
            FilteredElementCollector col = new FilteredElementCollector(doc);
            ICollection<Element> doors = col.OfCategory(BuiltInCategory.OST_Doors).ToElements();

            try
            {
                // Filtered element collector is iterable
                foreach (Element e in doors)
                {

                    FamilyInstance Fi = e as FamilyInstance;

                    Parameter getparam = Fi.LookupParameter("Comments");

                    using (Transaction tx = new Transaction(doc, "getparam"))
                    {
                        tx.Start("Parameter Exchange");

                        //set parameter
                        string s = Fi.FromRoom.Name;
                        getparam.Set(s);
                        tx.Commit();
                    }

                }
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                // Code did not execute successfully, display exception error
                TaskDialog.Show("Exception Caught", "Exception: " + ex);
                // return Failed
                return Result.Failed;
            }
        }
    }
}

 

 

 

 

0 Likes
Accepted solutions (1)
1,469 Views
2 Replies
Replies (2)
Message 2 of 3

RPTHOMAS108
Mentor
Mentor
Accepted solution

NullReferenceException is the most popular exception by far and a personal favourite of mine.

 

I assume it is occurring at a location where you’ve declared an object but not given it a value such as:
FamilyInstance Fi = e as FamilyInstance

 

Your collector has FamilySymbols and FamilyInstances as you’ve not set .WhereElementIsNotElementType. Traditionally this would cause InvalidCastException (my second favourite exception) but with the ‘as’ keyword it masks the problem resulting in a null.

 

So when you later use a member of Fi you get NullReferenceException.

Message 3 of 3

T52K-BIB
Enthusiast
Enthusiast

@RPTHOMAS108  Thanks now it's😃 Working!!.

#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
#endregion

namespace COBie_Door_Space
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;
            
 
             // Use OfCategory method to apply an ElementCategoryFilter and fine elements in the Doors category
            FilteredElementCollector col = new FilteredElementCollector(doc);
            ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
            IList <Element> doors = col.WherePasses(filter).WhereElementIsNotElementType().ToElements();

            try
            {
                // Filtered element collector is iterable
                foreach (Element e in doors)
                {

                  Parameter getparam = e.LookupParameter("COBie.Component.Space");

                    using (Transaction tx = new Transaction(doc, "getparam"))
                    {
                        tx.Start("Parameter Exchange");

                        //set parameter
                        FamilyInstance Fi = e as FamilyInstance;
                        string s = Fi.FromRoom.Name;
                        getparam.Set(s);
                        tx.Commit();
                    }

                }
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                // Code did not execute successfully, display exception error
                TaskDialog.Show("Exception Caught", "Exception: " + ex);
                // return Failed
                return Result.Failed;
            }
        }
    }
}
0 Likes