# callFunction throwing ParticleCloudException

**URL:** <https://community.particle.io/t/callfunction-throwing-particlecloudexception/34890>\
**Category:** Developer Tools\
**Created:** [July 27, 2017, 3:09pm UTC](https://community.particle.io/t/callfunction-throwing-particlecloudexception/34890 "2017-07-27T15:09:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shapy](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@Shapy](https://community.particle.io/u/Shapy)\
**Post date:** [July 27, 2017, 3:09pm UTC](https://community.particle.io/t/callfunction-throwing-particlecloudexception/34890/1 "2017-07-27T15:09:34Z")

</div>

Hello There!  
I was testing my newly made android app using the Android SDK for my project to switch on or off an led. Here is my code

```
public class MainActivity extends AppCompatActivity {

    //Variables...
    ArrayList<Integer> watt = new ArrayList<>();
    int sumwatt = 0;
    TextView txt;
    private ToggleButton button;
  // RelativeLayout layout = (RelativeLayout) findViewById(R.id.Relative);
    private ParticleDevice myDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (ToggleButton) findViewById(R.id.tb);

        //txt = (TextView)findViewById(R.id.tex2);
        ParticleCloudSDK.init(getApplicationContext());

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    ParticleCloudSDK.getCloud().logIn("emailID", "password");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"Connecting...", Toast.LENGTH_LONG).show();
                        }
                    });

                    myDevice = ParticleCloudSDK.getCloud().getDevices().get(0);
                    Log.d("Test1","Device ID:"+myDevice.getID());
                    final String nameString = myDevice.getName();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("TESTING","Device Status:"+myDevice.getStatus());
                            if (myDevice.getStatus().equals("normal")){
                                Toast.makeText(getApplicationContext(), "Logged IN!", Toast.LENGTH_LONG).show();
                            }else{Toast.makeText(getApplicationContext(), "Not Connected!", Toast.LENGTH_LONG).show();}
                            Toast.makeText(getApplicationContext(), nameString + " is back!", Toast.LENGTH_LONG).show();
                        }
                    });
                } catch (ParticleCloudException e) {
                    e.printStackTrace();
                }

            }
        });

        t.start();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    onToggleClick(v);
                } catch (ParticleCloudException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParticleDevice.FunctionDoesNotExistException e) {
                    e.printStackTrace();
                } catch (ParticleDevice.VariableDoesNotExistException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void onToggleClick(View view) throws ParticleCloudException,IOException,ParticleDevice.FunctionDoesNotExistException,ParticleDevice.VariableDoesNotExistException {

        if (button.isChecked() == true) {
            //Board is Switched ON
            Log.d("Test2","Available functions:"+myDevice.getFunctions());

            int result = myDevice.callFunction("power", list("on"));
            Log.d("Test3","Recieved result="+result);
            if (result == 1) {
                Toast.makeText(this, "Power ON!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Not Working!!!", Toast.LENGTH_SHORT).show();
            }

        } else if (button.isChecked() == false) {
            //Board is Switched OFF
            Log.d("Test2","Available functions:"+myDevice.getFunctions());
            int result = myDevice.callFunction("power", list("off"));
            Log.d("Test3","Recieved result="+result);
            if (result == 0) {
                Toast.makeText(this, "Power OFF!", Toast.LENGTH_SHORT).show();
                int wattage = myDevice.getIntVariable("wattage");
                watt.add(wattage);

                //Array wattage usage values...
                Integer[] watts = watt.toArray(new Integer[0]);

                //Calculating net watts...
                for (int x : watts) {
                    sumwatt += x;
                }

                //Displaying net watts consumed...
                txt.setText("Net Wattage:" + sumwatt);

            }
        }
    }
}

```

My device is connecting as my log message is showing getstatus() result as “normal”(idk if this tells if the device and app are connected, if I am wrong please do tell me!). Now when I am pressing my toggle switch  
, my log monitor is showing me this message

```
07-27 20:27:16.580 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch D/Test2: Available functions:[power]
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: io.particle.android.sdk.cloud.ParticleCloudException
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at io.particle.android.sdk.cloud.ParticleDevice.callFunction(ParticleDevice.java:365)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at com.a09gmail.a23.adityaprakash.particleswitch.MainActivity.onToggleClick(MainActivity.java:105)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at com.a09gmail.a23.adityaprakash.particleswitch.MainActivity$2.onClick(MainActivity.java:84)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.view.View.performClick(View.java:5198)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.widget.CompoundButton.performClick(CompoundButton.java:122)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.view.View$PerformClick.run(View.java:21147)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.os.Looper.loop(Looper.java:148)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at java.lang.reflect.Method.invoke(Native Method)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-27 20:27:16.586 14700-14700/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

```

It is throwing a ParticleCloudException whenever I am trying to call my function “power”. Please help me to solve this problem.  
Thanks!

---

<div class="post-metadata">

**Author:** ![julius](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/julius/32/13435_2.png) [@julius](https://community.particle.io/u/julius)\
**Post date:** [July 27, 2017, 5:16pm UTC](https://community.particle.io/t/callfunction-throwing-particlecloudexception/34890/2 "2017-07-27T17:16:18Z")

</div>

Looks like you are calling “callFunction” on main thread. “CallFunction” has to be called in separate thread same as “ParticleCloudSDK.getCloud().logIn()”.

---

<div class="post-metadata">

**Author:** ![Shapy](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@Shapy](https://community.particle.io/u/Shapy)\
**Post date:** [July 28, 2017, 6:33am UTC](https://community.particle.io/t/callfunction-throwing-particlecloudexception/34890/3 "2017-07-28T06:33:10Z")

</div>

Hi Julius!  
I tried what you suggested.This is the change in my function code

```
public void onToggleClick(View view) throws ParticleCloudException,IOException,ParticleDevice.FunctionDoesNotExistException,ParticleDevice.VariableDoesNotExistException {

        if (button.isChecked() == true) {
            //Board is Switched ON
            Thread t = new Thread(new Runnable(){
                public void run(){
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Log.d("Test2","Available functions:"+myDevice.getFunctions());
                                    int result = myDevice.callFunction("power", list("on"));
                                    Log.d("Test3", "Recieved result=" + result);

                                }catch(ParticleDevice.FunctionDoesNotExistException e){e.printStackTrace();}
                                 catch(IOException e){e.printStackTrace();}
                                 catch(ParticleCloudException e){e.printStackTrace();}
                            }
                        });
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
            }
            });
            t.start();

        }else if (button.isChecked() == false) {
            //Board is Switched OFF
            final Thread t = new Thread(new Runnable(){
                public void run(){
                    try {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Log.d("Test2","Available functions:"+myDevice.getFunctions());
                                    int result = myDevice.callFunction("power", list("off"));
                                    Log.d("Test3", "Recieved result=" + result);
                                    if (result == 0) {
                                        int wattage = myDevice.getIntVariable("wattage");
                                        watt.add(wattage);

                                        //Array wattage usage values...
                                        Integer[] watts = watt.toArray(new Integer[0]);
                                        //Calculating net watts...
                                        for (int x : watts) {
                                            sumwatt += x;
                                        }
                                        //Displaying net watts consumed...
                                        txt.setText("Net Wattage:" + sumwatt);
                                        }
                                }catch(ParticleDevice.FunctionDoesNotExistException e){e.printStackTrace();}
                                 catch(IOException e){e.printStackTrace();}
                                 catch(ParticleCloudException e){e.printStackTrace();}
                                 catch(ParticleDevice.VariableDoesNotExistException e){e.printStackTrace();}
                            }
                        });
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }

```

But it’s still throwing the same ParticleCloudException

```
07-28 11:47:09.942 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch D/Test2: Available functions:[power]
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: io.particle.android.sdk.cloud.ParticleCloudException
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at io.particle.android.sdk.cloud.ParticleDevice.callFunction(ParticleDevice.java:365)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at com.a09gmail.a23.adityaprakash.particleswitch.MainActivity$4$1.run(MainActivity.java:137)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.os.Looper.loop(Looper.java:148)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at java.lang.reflect.Method.invoke(Native Method)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-28 11:47:09.947 10319-10319/com.a09gmail.a23.adityaprakash.particleswitch W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

```

What should I do to solve this?  
Thanks!

---

<div class="post-metadata">

**Author:** ![julius](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/julius/32/13435_2.png) [@julius](https://community.particle.io/u/julius)\
**Post date:** [July 28, 2017, 8:53am UTC](https://community.particle.io/t/callfunction-throwing-particlecloudexception/34890/4 "2017-07-28T08:53:47Z")

</div>

Still on main thread - "runOnUiThread" runs code on main/UI thread. I suggest using [particle async task](https://docs.particle.io/reference/android/#sdk-calls-from-the-ui-thread).

> public void onToggleClick(View view) throws ParticleCloudException,IOException,ParticleDevice.FunctionDoesNotExistException,ParticleDevice.VariableDoesNotExistException {

> ```
> if (button.isChecked() == true) {
> //Board is Switched ON
> Thread t = new Thread(new Runnable(){
> public void run(){
> try {
> Log.d("Test2","Available functions:"+myDevice.getFunctions());
> int result = myDevice.callFunction("power", list("on"));
> Log.d("Test3", "Recieved result=" + result);
> 
> ```

> ```
> }catch(ParticleDevice.FunctionDoesNotExistException e){e.printStackTrace();}
> catch(IOException e){e.printStackTrace();}
> catch(ParticleCloudException e){e.printStackTrace();}
> }
> });
> t.start();
> 
> ```

> ```
> }else if (button.isChecked() == false) {
> //Board is Switched OFF
> final Thread t = new Thread(new Runnable(){
> public void run(){
> try {
> Log.d("Test2","Available functions:"+myDevice.getFunctions());
> int result = myDevice.callFunction("power", list("off"));
> Log.d("Test3", "Recieved result=" + result);
> if (result == 0) {
> int wattage = myDevice.getIntVariable("wattage");
> watt.add(wattage);
> 
> ```

> ```
> //Array wattage usage values...
> Integer[] watts = watt.toArray(new Integer[0]);
> //Calculating net watts...
> for (int x : watts) {
> sumwatt += x;
> }
> //Displaying net watts consumed...
> txt.setText("Net Wattage:" + sumwatt);
> }
> }catch(ParticleDevice.FunctionDoesNotExistException e){e.printStackTrace();}
> catch(IOException e){e.printStackTrace();}
> catch(ParticleCloudException e){e.printStackTrace();}
> catch(ParticleDevice.VariableDoesNotExistException e){e.printStackTrace();}
> }
> });
> t.start();
> }
> }
> 
> ```
