title: Android JNI: Java calling a C++ class object via JNI
body: <p>Android Studio project:
  I am trying to call a C++ class object by passing a variable (integer,double...) from java and return a result (double) from the c++ Class Object using JNI interface. When I compile/build  the project, there was no error or warning, it compiled fine. However, when I ran the the apps, it crashed "Unfortunately, App_xxx has stopped". I have tracked down the error to the way I call the object (in java and NTI file). </p>
  
  <p>Did I called the object correctly from the MainActivity and in the JNI interface? Can anyone help or see what I did wrong with my code?</p>
  
  <p>Here's the code of the MainActivity java:</p>
  
  <pre><code>package com.abc.www.apps;
  
  import ...
  
  public class MainActivity extends AppCompatActivity {
  
  
     double ax, bx, cx;
     double totalA, totalB;
  
     // Used to load the 'native-lib' library on application startup.
     static {
        System.loadLibrary("native-lib");
     }
  
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        //Call object in .cpp file (C++)
        ax = 2;
        bx = 3;
        cx = 4;
        totalA = getSum(ax, bx, cx);
        totalB = getDiff(ax, cx);
  
        //Display results
        TextView tv = (TextView) findViewById(R.id.id_dispTotA);
        tv.setText(Double.toString(totalA));
  
        TextView tv = (TextView) findViewById(R.id.id_dispTotB);
        tv.setText(Double.toString(totalB));
     }
  
     /**
      * A native method that is implemented by the 'native-lib' native library,
      * which is packaged with this application.
     */
     public native double getSum(double ax, double bx, double cx);
     public native double getDiff(double ax, double cx);
  }
  </code></pre>
  
  <p>Here's the code of the native-lib.cpp:</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include &lt;string&gt;
  #include "mathResult.h"
  
  extern "C"
  JNIEXPORT jdouble JNICALL
  Java_com_abc_www_apps_MainActivity_getSum(
      JNIEnv *env, jobject,
      jfloat a, jfloat b, jfloat c) {
  
      jdouble value;
      mathResult m;
  
      value = m.getSum(a, b, c);
      return value;
  }
  
  JNIEXPORT jdouble JNICALL
  Java_com_abc_www_apps_MainActivity_getDiff(
      JNIEnv *env, jobject,
      jfloat a, jfloat c) {
  
      jdouble value;
      mathResult m;
  
      value = m.getDiff(a, c);
      return value;
  }
  </code></pre>
  
  <p>Here's the code of the C++ Class Object (.cpp):</p>
  
  <pre><code>#include "mathResult.h"
  #include "math.h"
  
  //Sum
  double mathResult::getSum(double a, double b, double c)
  {
     Sum = a + b + c;
     return Sum;
  }
  
  //Subtraction
  double mathResult::getDiff(double a, double c)
  {
     Diff = c - a;
     return Diff;
  }
  </code></pre>
  
  <p>Any help would be much appreciated, thanks.</p>