Universal Robots Forum

Global variables in a Singleton

Hi,

I want to store my global variables in a singleton, but now when i use my set/get it gives me a null.pointer.exception.
I now have my variables set/get on the installation node but when you switch the urp these variables will not be changed/saved…
because the installation is the same.
Can someone help me out?
Many thanks!

Variables stored in the installation Node should only be set by the installation Node itself. You can read them from your contributions by using the apiProvider.getProgramAPI().getInstallationNode(<yourinstallationnodecontribution.class>) Interface. Using a Singleton class can be handy if you want your contributions communicate with each other. For actually storing variables you should either use your datamodel inside your installation or program nodes.

Thanks for your response!

I have my global variables stored in the datamodel of the installation node, but the problem is that when you save the program to an .urp the global variables will not be saved.
and when you change them and go back to your saved program, the global variables are not the same.

that’s how it’s intended to be because a .urp program is bound to a .installation which in your case should then also be modified. Whats your task? Do you want to modify global (URScript) variables with each program run?

I have multiple of the same urcap program nodes and when I change the variables on one, I want all of them to change. (because they are the same). So I think the variables don’t need to be “global” but connected with eachother.
it works when I get/set the variables in the installation node but I don’t want them changed everytime I open a previously saved .urp
So I think it’s better to use a singleton because that way I theoretically connect my contributions but I get a null.pointer.exception when i try to get or set my variables.

Ok in this case a singleton can solve your issue. The thing is how to actually trigger the other unselected program nodes to save a changed variable from another program node. I think what should work is to call your singleton get method inside all program nodes isDefined() functions because this is the only one which is called by polyscope continously without opening the specific program node or executing the program (and trigger it with generateScript()). When your variable is changed call another method that implements your model.set() method for the specific program node and it should work fine.

maybe there’s a much simpler method for doing so. I never tried to use static class variables in contributions tho…

Ok!
I wil try this out.
Thanks for your help!

Um well… a static variable is indeed the solution.
Sorry for confusion sometimes the simple things are not the obvious :smiley:

Just make sure, you do not violate this one: :slight_smile: