VariableDoesNotExistException Android Studio with Particle

I’ve follwed the docs which state to get a variable you insert the following code into android studio.

double aDouble = myDevice.getDoubleVariable("someDoubleValue");

So cater for my variable I have declared and flashed onto my photon I’ve changed this to:

double longitude = particleDevice.getDoubleVariable("longitude");

However I’m getting this error (referring to the ("longitude")

Error:(56, 96) error: unreported exception VariableDoesNotExistException; must be caught or declared to be thrown

I don’t understand why this error would occur. Obviously the variable doesn’t exist yet because it is going to retrieve it from my photon, but it can’t until this code compiles!

Is your device online and the variable available?

yes my device is online and the variable exists within the code I flashed to the particle. I initialized it as double longitude = 0; just below my header declarations and above void setup { ... }.

It has nothing to do with the photon though as Android Studio won’t compile the code as the variable doesn’t exist within it. But that’s the whole idea of the getDoubleVariable() to fetch it.

The error also has written:

unhandled exception: io.particle.andriod.sdk.cloud.ParticleDevice.VariableDoesNotExistException

If you get an unhandled exception error, you should actually catch that exception in a try ... catch block.
Or when a function is expected to (re)throw execptions (since it’s not going to handle them internally), it must do so too.
The same goes for functions that are meant to return a return-value, all code paths - including exceptions - must do so.

Since this is not really a Particle specific question, I’d just refer you to some more general answer to that question

BTW, the mere existance of the Particle.variable() in your code does not really prove it’s actually available on the device - e.g. when your code isn’t really the one that’s running on the device.
To actually know whether it exists or not can be checked via console.particle.io/devices or particle list

Not sure if it has anything to do with it or not, but I noticed in your title and your error that you write “Android” as “Andriod”. You might want to correct that.

1 Like

I discovered it wasnt compiling as I needed to write:

try {
....
} catch(ParticleDevice.VariableDoesNotExistException e) 

although from my previous try {....} catch(ParticleDevice.FunctionDoesNotExistException e)

Clearly noting the difference being not changing Function to Variable when trying to catch something else.

1 Like