revit Addin Manager bring up System.NullReferenceException

revit Addin Manager bring up System.NullReferenceException

Chris-VC_Studio
Advocate Advocate
898 Views
5 Replies
Message 1 of 6

revit Addin Manager bring up System.NullReferenceException

Chris-VC_Studio
Advocate
Advocate

When running very basic code thru the add In Manager I keep getting a system.NullReferenceException. not sure what I am doing wrong it compiled without errors and up to 3 days ago everything ran well with no problems, but now when I try to run a simple FilteredElementCollector Method I keep getting this error and it won't load into Revit.

Screen Shot 2022-12-17 at 12.57.26 PM.png

 

using Autodesk.Revit.DB;

using Autodesk.Revit.UI;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Autodesk.Revit.ApplicationServices;

using Autodesk.Revit.Attributes;

 

namespace CoreRevitLibrary.TaskCommands

{

    [Transaction(TransactionMode.Manual)]

    [Regeneration(RegenerationOption.Manual)]

    public class SelectionCommand : IExternalCommand

    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)

        {

            var uiApplication = commandData.Application;

            var application = uiApplication.Application;

            var uidocument = uiApplication.ActiveUIDocument;

            var document = uidocument.Document;

 

            var collector = new FilteredElementCollector(document)

                .OfCategory(BuiltInCategory.OST_Walls).ToElements();

            TaskDialog.Show("Message", collector.Count.ToString());

 

            return Result.Succeeded;

        }

    }

}

0 Likes
Accepted solutions (2)
899 Views
5 Replies
Replies (5)
Message 2 of 6

moturi.magati.george
Autodesk
Autodesk

Hi @Chris-VC_Studio,

 

This seems strange, I would suggest you run this code in debug mode and add a breakpoint on the line "var uiApplication = commandData.Application;". It would give you an idea if this code is triggering the error or it is triggered before the addin is executed.

  Moturi George,     Developer Advocacy and Support,  ADN Open
0 Likes
Message 3 of 6

Chris-VC_Studio
Advocate
Advocate

Tried it, still no luck, it still would load and I got the same issue. 

Now let add a twist to this mystery, I copied from one computer to another PC and rebuilt the solution, then re-ran the solution and everything worked perfectly. But when I tried to bring the same solution back to my regular coding PC, I still get the same error; at least I know there is nothing wrong with the solution. 

0 Likes
Message 4 of 6

moturi.magati.george
Autodesk
Autodesk

Hi @Chris-VC_Studio,

 

 

Since the issue is specific to your installation. You could perhaps try to repair Revit Application and see if it will clear the error. Here are steps on how to do that:

 

https://knowledge.autodesk.com/support/autocad/learn-explore/caas/sfdcarticles/sfdcarticles/How-to-r...

  Moturi George,     Developer Advocacy and Support,  ADN Open
Message 5 of 6

jeremy_tammik
Alumni
Alumni
Accepted solution

I would suggest searching globally for your .NET assembly DLL defining and implementing the external command. It sound as if you might have a duplicate obsolete version lurking somewhere and are loading that inadvertently. Just as George suggests, stepping into and debugging the code would help reveal that problem, if it is the cause, because it will either not work or you will step into something completely different from what you expect.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 6 of 6

Chris-VC_Studio
Advocate
Advocate
Accepted solution

Thank you gentlemen for your help, and I believe the problem existed due to a duplicate version since I had the solution folder living in my Documents folder which is linked to my OneDrive with versioning control. After I copied the solution folder to my source\repos folder nested on my C-drive everything worked perfectly. I am back in business and ready to take on Great and Miraculous things once again.

 

0 Likes