How to send data to ubidots using a tooggle button on android studio?


#1

hi, im tragin to send data from a toggle button that i create on android studio but when y try to sen a 1 or a 0 to the server the app crash, here is the code

final ToggleButton B1=(ToggleButton)findViewById(R.id.toggleButton);
            B1.setOnClickListener(new View.OnClickListener(){
                public void onClick(View vv){
                    if(B1.isChecked()){
                        findViewById(R.id.toggleButton).setBackgroundResource(R.drawable.air_on);
                        ApiClient apiClient = new ApiClient(API_KEY);
                        Variable batteryLevel = apiClient.getVariable(VARIABLE_ID2);
                        batteryLevel.saveValue(1);

                    }
                    else{
                        findViewById(R.id.toggleButton).setBackgroundResource(R.drawable.air);
                        ApiClient apiClient = new ApiClient(API_KEY);
                        Variable batteryLevel = apiClient.getVariable(VARIABLE_ID2);
                        batteryLevel.saveValue(0);
                    }

                }

            });

The error that i get on from android is this

FATAL EXCEPTION: main
Process: com.example.camilobarco.domotic, PID: 5681
java.lang.NullPointerException: Attempt to invoke interface method ‘java.lang.Object java.util.Map.get(java.lang.Object)’ on a null object reference
at com.ubidots.ServerBridge.recieveToken(ServerBridge.java:72)
at com.ubidots.ServerBridge.initialize(ServerBridge.java:67)
at com.ubidots.ServerBridge.(ServerBridge.java:54)
at com.ubidots.ServerBridge.(ServerBridge.java:43)
at com.ubidots.ApiClient.(ApiClient.java:16)
at com.example.camilobarco.domotic.MainActivity$ApiUbidots$1.onClick(MainActivity.java:111)
at android.view.View.performClick(View.java:5612)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

can some please help me whit this,

Thanks a lot


#2

Hello @CamiloBarco,

Thank you… We will review your information and get back to you! :smiley: