Appsettings not accessible

smartapp

(Arisht Jain) #1

I have defined appSettings in my smartApp

{
        appSetting "appId"
        appSetting "apiToken"
    }

Though, appSettings variable is accessible anywhere but I can’t access my appId from it. I get the error
java.lang.NullPointerException: Cannot get property 'appId' on null object @line 24 (run) on accessing appSettings.appId.
However, I can also access the same inside initialize() function. Why can’t I access specific setting but can very well access appSettings variable as a whole?


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #2

Have you set a value for appId in the SmartApp properties?


(Arisht Jain) #3

Yes, the value is already set. I can even access that value in initialize() function scope.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #4

Try using a different name than “appId” – I think that is quite possibly a reserved word.

Another workaround is to copy it to the settings.* map in your initialize() method.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #5

FYI:

This line works fine inside an arbitrary method in one of my SmartApps:

def map = [
			uri: appSettings.apiPath + "/stuff.cgi?auth=${appSettings.apiKey}", 
...

(Arisht Jain) #6

Thanks for the workaround but the workaround to copy it to the settings.* map in my initialize() method won’t benefit me as I want to acces it in preferences block. Also, I can get the value in any method, just not outside it. Is there any way to access the value in preferences block.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #7

Ah… Preferences! … That’s the detail I was missing!

There are tremendous limitations as to what objects/variables are available in preferences{}. Preferences are actually metadata and not Groovy code / methods. This section is parsed by a preprocessor and/or rendering layer.

I frankly don’t think you can access appSettings within preferences{}. Along with a lot of other things you can’t do in preferences.

Carefully study the documentation section on Dynamic Preferences, just in case I’m missing something obvious. Dynamic Preferences are powerful, but still limited - and I’m not sure all the limitations are clearly documented: https://docs.smartthings.com/en/latest/smartapp-developers-guide/preferences-and-settings.html#dynamic-preferences


BTW: You’re also more likely to get a helpful answer here if you detail exactly what your goal is, rather than just the syntax you are attempting to use.

  • Why do you need appsettings in Preferences?