title: JNI NoSuchMethodException: parameter type is null
body: <p>I am working on calling an instance method using <code>JNI</code> in <code>Android</code>. All code compiles, yet to protect the content of the project, I only will show relevant parts of the code. The following code block (<code>extern c</code> used in a <em>c++</em> file) attempts to access a static method [<code>public String getMyString()</code>] in <em>cls</em>. I know that <em>cls</em> and <em>env</em> are not null. <em>mid</em> is not set to zero, nor is it <em>null</em>. When this native method returns to the Java side, I get the following error (traced back to <a href="http://source-android.frandroid.com/libcore/luni/src/main/java/java/lang/Class.java" rel="nofollow">Class.java</a>):</p>
  
  <pre><code>02-22 10:38:36.010: E/AndroidRuntime(17894): FATAL EXCEPTION: GLThread 10
  02-22 10:38:36.010: E/AndroidRuntime(17894): java.lang.NoSuchMethodException: parameter type is null
  02-22 10:38:36.010: E/AndroidRuntime(17894):    at java.lang.ClassMembers.getConstructorOrMethod(ClassMembers.java:228)
  02-22 10:38:36.010: E/AndroidRuntime(17894):    at java.lang.Class.getMethod(Class.java:904)
  02-22 10:38:36.010: E/AndroidRuntime(17894):    at com.example.android.MyClass.getMyString(Native Method)
  02-22 10:38:36.010: ...
  </code></pre>
  
  <p>The code that generates the error is as follows:</p>
  
  <pre><code>    jmethodID mid = env-&gt;GetMethodID(cls, "getMyString", "()Ljava/lang/String;");
      if (mid == 0) {
          __android_log_write(ANDROID_LOG_INFO, "JNI", "mid is 0");
      }
      else {
          __android_log_write(ANDROID_LOG_INFO, "JNI", "mid is not 0");
      }
      jstr = (jstring) env-&gt;CallObjectMethod(obj, mid);
      if (jstr == NULL) {
          __android_log_write(ANDROID_LOG_INFO, "JNI", "jstr is NULL");
      }
      else {
          __android_log_write(ANDROID_LOG_INFO, "JNI", "jstr is not NULL");
      }
  
      printf("  c.s = \"%s\"\n", env-&gt;GetStringUTFChars(jstr, NULL));
  
      return jstr;
  </code></pre>
  
  <p>Although Class.java seems to suggest it is a problem with the cast from <em>null</em> to <code>Class&lt;?&gt;[]</code> (common for reflection), I have a feeling it has more to do with <em>mid</em> finding the wrong method or <em>jstr</em> coming back incorrectly (logcat states that <em>jstr</em> is NOT null).</p>
  
  <p>Lastly, the Java-side relevant code is as follows:</p>
  
  <p>The native call:</p>
  
  <pre><code>public static native String getMyNativeString();
  </code></pre>
  
  <p>The call to the native method:</p>
  
  <pre><code>String myStr = callGetMyNativeString();
  </code></pre>
  
  <p>The method that is being accessed:</p>
  
  <pre><code>public String getMyString() {
      return "Foobar";
  }
  </code></pre>