0
votes

I am trying to execute connected test for P4, however I am reciing an "Null pointer exception error" for P4

Error message:

:00:02 PM null
java.lang.NullPointerException
at com.android.ddmlib.Client.read(Client.java:692)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:304)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:256)

It is a standard test, verifying non-empty string in the Async task

Test function:

public void runCloudModuleTest() {
    String joke = null;
    JokesAsyncTask jokesAsyncTask = new JokesAsyncTask(getContext(), null);
    jokesAsyncTask.execute();
    try {
        joke = jokesAsyncTask.get();
        Log.d("CloudModuleTest", "Retrieved a non-empty string successfully: " + joke);
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertNotNull(joke);
}

Can someone help me understand what the issue is?

AsyncTask: The Async task pulls data from google cloud engine

public class JokesAsyncTask extends AsyncTask, Void, String> {

private static JokeApi myApiService = null;
private Context mContext;
private String mResult;
private ProgressBar mProgressBar;

public JokesAsyncTask(Context context, ProgressBar progressBar) {
    this.mContext = context;
    this.mProgressBar = progressBar;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    if (mProgressBar != null) {
        mProgressBar.setVisibility(View.VISIBLE);
    }
}

@Override
protected String doInBackground(Pair<Context, String>... pairs) {
    if (myApiService == null) {
        JokeApi.Builder builder = new JokeApi.Builder(AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(), null)
                .setRootUrl("https://testandroiddevelopment.appspot.com/_ah/api/");
        myApiService = builder.build();
    }
    try {
        return myApiService.sendJoke(new JokeBean()).execute().getJoke();
    } catch (IOException e) {
        return e.getMessage();
    }
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    if (mProgressBar != null) {
        mProgressBar.setVisibility(View.GONE);
    }
    mResult = result;
    startJokeDisplayActivity();
}

private void startJokeDisplayActivity() {

    Intent intent = new Intent(mContext, JokeViewActivity.class);
    intent.putExtra(JokeViewActivity.JOKE_KEY, mResult);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mContext.startActivity(intent);
}

}

I have referenced the variable and it is not an issue due due to the below post, however I did investigate and finally cleaned up and rebuild the project that helped resolved the issue

1
What is printing null before the exception? It seems your AsyncTask is returning null, so show your JokesAsyncTask class - OneCricketeer
Added Asynctask class - Mahesh N
myApiService is OkHttp or Retrofit? Why do you need an AsyncTask for that? - OneCricketeer
I am doing one of the project with Udacity Android and the requirement is to create an Async task to retrieve data from GCE. MyAPI service is not okhttp or retrofit - Mahesh N
Well, I haven't used GCE, but I do know that you more than likely should be using the Async features of AsyncTask. By using the get() method on it, you've forced it back to synchronous code. Unfortunately, unit-testing asynchronous code doesn't make much sense. - OneCricketeer

1 Answers

0
votes

I have referenced the variable and it is not an issue due due to the post @AxelH, however I did investigate and finally cleaned up and rebuild the project that helped resolved the issue