Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

IsCommandAvailable causes nullref exception

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
Anonymous
679 Views, 10 Replies

IsCommandAvailable causes nullref exception

Anonymous
Not applicable

Ok, this is absolutely doing my head in as I have searched for countless examples all of which seem to suggest I'm not doing anything wrong... Basically, I just want to control the visibility/usability of my external command. Everywhere I've read suggests:

 

namespace HERibbon
{
    // [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class Availability : IExternalCommandAvailability
    {
        public bool IsCommandAvailable(UIApplication uiApp, CategorySet catSet)
        {
            // if (catSet.IsEmpty) return true;
            // if (uiApp.ActiveUIDocument.Document.IsFamilyDocument) return false;
            // if (uiApp.ActiveUIDocument.Document.IsModifiable) return true;
            return true;
        }
    }
    class App : IExternalApplication
    {
        static void AddRibbonPanel(UIControlledApplication application)
        {
            // Create the ribbon tab
            String tabName = @"H&E Tools";
            application.CreateRibbonTab(tabName);

            // Create the panels on that tab
            RibbonPanel rp_About = application.CreateRibbonPanel(tabName, "About");

            // Get our installation path
            string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;

            // Add to the About Panel
            // About
            PushButtonData pbData_About = new PushButtonData("About", "Info", thisAssemblyPath, "HETools.About");
            // pbData_About.AvailabilityClassName = "HERibbon.Availability";
            PushButton pb_About = rp_About.AddItem(pbData_About) as PushButton;
            pb_About.ToolTip = @"H&E Tools version info.";
            BitmapImage image_About = new BitmapImage(new Uri(@"C:\Program Files\HETools\src\about.ico"));
            pb_About.LargeImage = image_About;
            pb_About.Image = image_About;
            pb_About.AvailabilityClassName = "HERibbon.Availability";
        }

        public Result OnStartup(UIControlledApplication application)
        {
            AddRibbonPanel(application);
            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }
    }
}

The commented out code are combos I've tried to get this thing to work.

 

The problem is when I select my tab on the ribbon when no document is open I get the following error: 

Capture.PNG

 

Debug says the exception is thrown at the public bool IsCommandAvailable. If I comment out the class, my plugin works fine. Even with the exception, the plugins still work once a project is opened, however, I need to fix this. My main plugin is a workshare enabler that I obviously can't have being run in the family editor (for example).

 

I have literally tried copying and pasting this sample and I still get a null ref error. I started from thebuildingcoder's zerodoc and am getting the same exception. I am absolutely out of ideas... has something changed in 2019?

0 Likes

IsCommandAvailable causes nullref exception

Ok, this is absolutely doing my head in as I have searched for countless examples all of which seem to suggest I'm not doing anything wrong... Basically, I just want to control the visibility/usability of my external command. Everywhere I've read suggests:

 

namespace HERibbon
{
    // [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class Availability : IExternalCommandAvailability
    {
        public bool IsCommandAvailable(UIApplication uiApp, CategorySet catSet)
        {
            // if (catSet.IsEmpty) return true;
            // if (uiApp.ActiveUIDocument.Document.IsFamilyDocument) return false;
            // if (uiApp.ActiveUIDocument.Document.IsModifiable) return true;
            return true;
        }
    }
    class App : IExternalApplication
    {
        static void AddRibbonPanel(UIControlledApplication application)
        {
            // Create the ribbon tab
            String tabName = @"H&E Tools";
            application.CreateRibbonTab(tabName);

            // Create the panels on that tab
            RibbonPanel rp_About = application.CreateRibbonPanel(tabName, "About");

            // Get our installation path
            string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;

            // Add to the About Panel
            // About
            PushButtonData pbData_About = new PushButtonData("About", "Info", thisAssemblyPath, "HETools.About");
            // pbData_About.AvailabilityClassName = "HERibbon.Availability";
            PushButton pb_About = rp_About.AddItem(pbData_About) as PushButton;
            pb_About.ToolTip = @"H&E Tools version info.";
            BitmapImage image_About = new BitmapImage(new Uri(@"C:\Program Files\HETools\src\about.ico"));
            pb_About.LargeImage = image_About;
            pb_About.Image = image_About;
            pb_About.AvailabilityClassName = "HERibbon.Availability";
        }

        public Result OnStartup(UIControlledApplication application)
        {
            AddRibbonPanel(application);
            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }
    }
}

The commented out code are combos I've tried to get this thing to work.

 

The problem is when I select my tab on the ribbon when no document is open I get the following error: 

Capture.PNG

 

Debug says the exception is thrown at the public bool IsCommandAvailable. If I comment out the class, my plugin works fine. Even with the exception, the plugins still work once a project is opened, however, I need to fix this. My main plugin is a workshare enabler that I obviously can't have being run in the family editor (for example).

 

I have literally tried copying and pasting this sample and I still get a null ref error. I started from thebuildingcoder's zerodoc and am getting the same exception. I am absolutely out of ideas... has something changed in 2019?

10 REPLIES 10
Message 2 of 11
jeremytammik
in reply to: Anonymous

jeremytammik
Autodesk
Autodesk

Dear John,

 

Thank you for your query.

 

Welcome to the digital world of programming.

 

Unfortunately, the computer will always just do what you tell it to.

 

Not what you want.

 

It normally takes a decade or two for a novice programmer to accept and get used to that hard fact.

 

Barring bugs in the libraries you use, of course, including and not limited to the Revit API.

 

Therefore, I personally prefer to DIY and just fight with my own bugs, whenever I can.

 

First of all, debug your code step by step to discover exactly which line throws the exception, so you can identify the null reference culprit. That will probably clear the problem right away.

  

Next: have you looked at this old sample of mine?

 

http://thebuildingcoder.typepad.com/blog/2011/02/enable-ribbon-items-in-zero-document-state.html

 

If that does not help, I would suggest the next step: search globally for IExternalCommandAvailability in the official Revit SDK samples, then in The Building Coder blog, then on the Internet in general.

 

I am sure there is a really easy fix, which will be totally obvious once you discover it.

 

I hope this helps.

 

Good luck!

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes

Dear John,

 

Thank you for your query.

 

Welcome to the digital world of programming.

 

Unfortunately, the computer will always just do what you tell it to.

 

Not what you want.

 

It normally takes a decade or two for a novice programmer to accept and get used to that hard fact.

 

Barring bugs in the libraries you use, of course, including and not limited to the Revit API.

 

Therefore, I personally prefer to DIY and just fight with my own bugs, whenever I can.

 

First of all, debug your code step by step to discover exactly which line throws the exception, so you can identify the null reference culprit. That will probably clear the problem right away.

  

Next: have you looked at this old sample of mine?

 

http://thebuildingcoder.typepad.com/blog/2011/02/enable-ribbon-items-in-zero-document-state.html

 

If that does not help, I would suggest the next step: search globally for IExternalCommandAvailability in the official Revit SDK samples, then in The Building Coder blog, then on the Internet in general.

 

I am sure there is a really easy fix, which will be totally obvious once you discover it.

 

I hope this helps.

 

Good luck!

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 11
Anonymous
in reply to: jeremytammik

Anonymous
Not applicable

Hi Jeremy,

 

Thanks for the reply. Per my post, I have debugged and the exception is thrown at the public bool.

 

And yes, again per my post, I have copied your example verbatim, and it threw the exact same error (hence me wondering if something has changed in 2019).

 

Appreciate the help.

0 Likes

Hi Jeremy,

 

Thanks for the reply. Per my post, I have debugged and the exception is thrown at the public bool.

 

And yes, again per my post, I have copied your example verbatim, and it threw the exact same error (hence me wondering if something has changed in 2019).

 

Appreciate the help.

Message 4 of 11
jeremytammik
in reply to: Anonymous

jeremytammik
Autodesk
Autodesk

I'll see whether I can get it running in Revit 2019 for you...

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes

I'll see whether I can get it running in Revit 2019 for you...

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 11
FAIR59
in reply to: Anonymous

FAIR59
Advisor
Advisor

there is a setting in the addin-manifest file [ NotVisibleWhenNoActiveDocument ] that will disable your  button when there is no document!

http://thebuildingcoder.typepad.com/blog/2010/05/addin-visibility-mode.html

0 Likes

there is a setting in the addin-manifest file [ NotVisibleWhenNoActiveDocument ] that will disable your  button when there is no document!

http://thebuildingcoder.typepad.com/blog/2010/05/addin-visibility-mode.html

Message 6 of 11
jeremytammik
in reply to: Anonymous

jeremytammik
Autodesk
Autodesk

I migrated the ZeroDocPanel to Revit 2019 for you and encountered no problem with that whatsoever.

 

I debugged it, and the OnStartup and IsCommandAvailable methods are both called just as expected.

 

The external command is listed and can be executed in the external tools menu in zero document state:

 zero_doc_button_2019.png

  

 

The command execution displays the task dialogue:

 zero_doc_cmd_msg_2019.png

  

 

So your problem is elsewhere, and you can restart from scratch with my updated sample to avoid it.

 

Good luck!

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes

I migrated the ZeroDocPanel to Revit 2019 for you and encountered no problem with that whatsoever.

 

I debugged it, and the OnStartup and IsCommandAvailable methods are both called just as expected.

 

The external command is listed and can be executed in the external tools menu in zero document state:

 zero_doc_button_2019.png

  

 

The command execution displays the task dialogue:

 zero_doc_cmd_msg_2019.png

  

 

So your problem is elsewhere, and you can restart from scratch with my updated sample to avoid it.

 

Good luck!

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 7 of 11
jeremytammik
in reply to: Anonymous

jeremytammik
Autodesk
Autodesk

Maybe you would like me to add the link?

 

https://github.com/jeremytammik/ZeroDocPanel

 

🙂

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes

Maybe you would like me to add the link?

 

https://github.com/jeremytammik/ZeroDocPanel

 

🙂

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 8 of 11
jeremytammik
in reply to: Anonymous

jeremytammik
Autodesk
Autodesk

Here is the blog post pointing out the update and the new “ZeroDocPanel GitHub repository:

 

http://thebuildingcoder.typepad.com/blog/2018/09/support-dlls-zero-document-state-and-forge-accelera...

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Here is the blog post pointing out the update and the new “ZeroDocPanel GitHub repository:

 

http://thebuildingcoder.typepad.com/blog/2018/09/support-dlls-zero-document-state-and-forge-accelera...

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 9 of 11
Anonymous
in reply to: FAIR59

Anonymous
Not applicable

Hi Jeremy,

 

I've downloaded your file, but haven't had the chance to implement it as yet (projects getting in the way). Given I have tried copying your code verbatim, I suspect the problem may lay elsewhere. I'll try commenting everything out and debugging one step at a time to see if I can isolate the cause.

 

I do appreciate you taking the time to have a look.

 

FAIR59 re: manifest file - I'm aware of this, though it is limiting. The plugins I've written - and one I have planned - will require a bit more flexibility. I'll need to get this working basically...

0 Likes

Hi Jeremy,

 

I've downloaded your file, but haven't had the chance to implement it as yet (projects getting in the way). Given I have tried copying your code verbatim, I suspect the problem may lay elsewhere. I'll try commenting everything out and debugging one step at a time to see if I can isolate the cause.

 

I do appreciate you taking the time to have a look.

 

FAIR59 re: manifest file - I'm aware of this, though it is limiting. The plugins I've written - and one I have planned - will require a bit more flexibility. I'll need to get this working basically...

Message 10 of 11
Anonymous
in reply to: Anonymous

Anonymous
Not applicable
Accepted solution

I don't know why, but for some reason changing the UIControlledApplication var name from "application" to "uiApp" inexplicably fixed the issue.

public bool IsCommandAvailable(UIApplication uiApp, CategorySet catSet)
static void AddRibbonPanel(UIControlledApplication application)
public Result OnStartup(UIControlledApplication application)
public Result OnShutdown(UIControlledApplication application)

Bizarre.

0 Likes

I don't know why, but for some reason changing the UIControlledApplication var name from "application" to "uiApp" inexplicably fixed the issue.

public bool IsCommandAvailable(UIApplication uiApp, CategorySet catSet)
static void AddRibbonPanel(UIControlledApplication application)
public Result OnStartup(UIControlledApplication application)
public Result OnShutdown(UIControlledApplication application)

Bizarre.

Message 11 of 11
jeremytammik
in reply to: Anonymous

jeremytammik
Autodesk
Autodesk

Dear John,

 

Thank you for your confirmation and congratulations to hear it is working now, however bizarre the resolution.

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes

Dear John,

 

Thank you for your confirmation and congratulations to hear it is working now, however bizarre the resolution.

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report