Merge "Fix b/160761473 - Broken test: android.nativemidi.cts.NativeMidiEchoTest..."
diff --git a/tests/tests/nativemidi/jni/native-lib.cpp b/tests/tests/nativemidi/jni/native-lib.cpp
index f482eee..5a2f0ca 100644
--- a/tests/tests/nativemidi/jni/native-lib.cpp
+++ b/tests/tests/nativemidi/jni/native-lib.cpp
@@ -278,6 +278,8 @@
 
 JNIEXPORT jlong JNICALL Java_android_nativemidi_cts_NativeMidiEchoTest_allocTestContext(
         JNIEnv* env, jclass) {
+    __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "allocTestContext()");
+
     TestContext* context = new TestContext;
     if (!context->initN(env)) {
         delete context;
@@ -289,6 +291,7 @@
 
 JNIEXPORT void JNICALL Java_android_nativemidi_cts_NativeMidiEchoTest_freeTestContext(
         JNIEnv*, jclass, jlong context) {
+    __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "freeTestContext()");
     delete (TestContext*)context;
 }
 
@@ -469,6 +472,11 @@
 
     // __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "++++ startReadingMidi()");
     TestContext* context = (TestContext*)ctx;
+    if (context == nullptr) {
+        __android_log_print(ANDROID_LOG_INFO, LOG_TAG,
+                "Test Context is null in  startReadingMidi()");
+        return AMEDIA_ERROR_INVALID_OBJECT;
+    }
 
     AMidiOutputPort* outputPort;
     media_status_t status = AMidiOutputPort_open(context->nativeDevice, portNumber, &outputPort);
@@ -494,6 +502,11 @@
 
     // __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "++++ stopReadingMidi()");
     TestContext* context = (TestContext*)ctx;
+    if (context == nullptr) {
+        __android_log_print(ANDROID_LOG_INFO, LOG_TAG,
+                "Test Context is null in  stopReadingMidi()");
+        return AMEDIA_ERROR_INVALID_OBJECT;
+    }
     context->mReading = false;
 
     context->mReadThread->join();