[SOLVED] Android SDK example crashes

Hello, i tried this tutorial for interacting with ubidots:
https://ubidots.com/docs/devices/android.html#android-sdk

but both the above and a modified version to suit my needs kept crashing on the ApiUbidots AsyncTask call as i see when running the app with breakpoints.

What i did:
-edited the gradle as suggested in the link
-edited the manifest as suggested in the link
-edited the layout and activity as suggested to the link with the only difference the use of ConstraintLayout
What i didn’t:
-add some fancy tags on the Ubidots website since the UI is very different now from what it was on the tutorial

The token and variable id are probably correct since i use the very same to upload the variables from an arduino to that ubidots device.

Android Studio Version: 2.3.2
Android Version : 4.4.2

[code]import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

import com.ubidots.*;

public class MainActivity extends Activity {
private TextView mAlertTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAlertTextView = (TextView) findViewById(R.id.alert);
    new ApiUbidots().execute();
}

public class ApiUbidots extends AsyncTask<Void, Void, Value[]> {
    private final String API_KEY = "TOKEN_HERE";
    private final String ALERT_ID = "VAR_ID_HERE";

    @Override
    protected Value[] doInBackground(Void... params) {
        ApiClient apiClient = new ApiClient(API_KEY);
        Variable alert = apiClient.getVariable(ALERT_ID);

        return alert.getValues();
    }

    @Override
    protected void onPostExecute(Value[] variableValues) {
        mAlertTextView.setText(String.valueOf(variableValues[0]));
    }
}

}[/code]

Thanks in advance for any help

Hi! How are you?

When you run this program and it crashes, Android Studio gives you a log with the stack trace. Can you please copy it and paste it here?

This would be helpful to check what could be wrong.

Thanks :smiley:

Hello, i’m good, you? Thanks for replying, here’s the stack trace:

08-19 17:14:02.040 23260-23395/com.example.dimitris.securino D/dalvikvm: threadid=14 (AsyncTask #2): calling run()
08-19 17:14:02.041 23260-23381/com.example.dimitris.securino W/System.err: java.lang.RuntimeException: An error occured while executing doInBackground()
08-19 17:14:02.042 23260-23381/com.example.dimitris.securino W/System.err: at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-19 17:14:02.043 23260-23381/com.example.dimitris.securino W/System.err: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-19 17:14:02.044 23260-23381/com.example.dimitris.securino W/System.err: at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-19 17:14:02.045 23260-23381/com.example.dimitris.securino W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-19 17:14:02.046 23260-23381/com.example.dimitris.securino W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-19 17:14:02.046 23260-23381/com.example.dimitris.securino W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-19 17:14:02.047 23260-23381/com.example.dimitris.securino W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-19 17:14:02.048 23260-23381/com.example.dimitris.securino W/System.err: at java.lang.Thread.run(Thread.java:841)
08-19 17:14:02.049 23260-23381/com.example.dimitris.securino W/System.err: Caused by: java.lang.NullPointerException
08-19 17:14:02.050 23260-23381/com.example.dimitris.securino W/System.err: at com.example.dimitris.securino.MainActivity$ApiUbidots.doInBackground(MainActivity.java:37)
08-19 17:14:02.051 23260-23381/com.example.dimitris.securino W/System.err: at com.example.dimitris.securino.MainActivity$ApiUbidots.doInBackground(MainActivity.java:28)
08-19 17:14:02.052 23260-23381/com.example.dimitris.securino W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-19 17:14:02.053 23260-23381/com.example.dimitris.securino W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-19 17:14:02.053 23260-23381/com.example.dimitris.securino W/System.err: … 4 more
08-19 17:14:02.053 23260-23381/com.example.dimitris.securino W/dalvikvm: threadid=11: calling UncaughtExceptionHandler
08-19 17:14:02.063 23260-23381/com.example.dimitris.securino E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.dimitris.securino, PID: 23260
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.dimitris.securino.MainActivity$ApiUbidots.doInBackground(MainActivity.java:37)
at com.example.dimitris.securino.MainActivity$ApiUbidots.doInBackground(MainActivity.java:28)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
08-19 17:14:02.328 23260-23260/com.example.dimitris.securino D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{427b8488 token=android.os.BinderProxy@427b7ba8 {com.example.dimitris.securino/com.example.dimitris.securino.MainActivity}}
08-19 17:14:02.378 23260-23260/com.example.dimitris.securino D/ActivityThread: ACT-PAUSE_ACTIVITY_FINISHING handled : 0 / android.os.BinderProxy@427b7ba8
08-19 17:14:02.501 23260-23260/com.example.dimitris.securino D/OpenGLRenderer: Flushing caches (mode 0)
08-19 17:14:02.502 23260-23260/com.example.dimitris.securino D/GraphicBuffer: close handle(0x61865468) (w:720 h:1280 f:1)
08-19 17:14:02.528 23260-23260/com.example.dimitris.securino D/OpenGLRenderer: Flushing caches (mode 1)
08-19 17:14:02.548 23260-23260/com.example.dimitris.securino D/OpenGLRenderer: Flushing caches (mode 0)
08-19 17:14:02.566 23260-23260/com.example.dimitris.securino D/ActivityThread: ACT-DESTROY_ACTIVITY handled : 1 / android.os.BinderProxy@427b7ba8

Seems to be a null pointer exception caused on line 37 “return alert.getValues();” but i just can’t pinpoint why, do i get an empty response from ubidots?

Let me check why are you getting a NullPointerException, I’m going to try to reproduce this error and will be back with a response.

Even I tried the same tutorial, the app crashes. Do provide a solution for it.

Greetings, the actual android sdk is outdated, we have released a native app that you can find at the play store, I gently invite you to test it.

All the best

But. I don’t want it only for visualizing. I want to send custom data from my phone app to the cloud which isn’t possible from the mentioned app.

You can create your own requests that fit our REST API using the HttpUrlConnection Java Class or any HTTP class available in kotlin. I think that it would valuable to create a help article for sending data using native Java classes, so I will ask to the development team to work on it in the next weeks.

All the best

I have been successful in sending the data and retrieving it via mqtt but not by the android sdk provided. I will soon create a help article regarding the same.