0

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

Mahesh N
  • 772
  • 1
  • 10
  • 21
  • What is printing `null` before the exception? It seems your AsyncTask is returning null, so show your `JokesAsyncTask` class – OneCricketeer Nov 08 '16 at 19:31
  • Added Asynctask class – Mahesh N Nov 08 '16 at 20:26
  • `myApiService` is OkHttp or Retrofit? Why do you need an AsyncTask for that? – OneCricketeer Nov 08 '16 at 20:27
  • 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 Nov 08 '16 at 20:34
  • Well, I haven't used GCE, but I do know that you more than likely should be using the Async features of **Async**Task. 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 Nov 08 '16 at 20:42
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – AxelH Nov 09 '16 at 08:00

1 Answers1

0

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

Mahesh N
  • 772
  • 1
  • 10
  • 21