Search code examples
javamatlabmatlab-compilerjavabuilders

Send and Receive JSON Objects to and from JAVA to MATLAB


I am using javabuilder tool of MATLAB to execute MATLAB Native Code on MATLAB runtime environment.

Code

public String getCHINdex(String tsDataJSON){
  MWCharArray number1 = null;
  Object[] result = null;
  Calibration calibration = null;

  try
  {
     number1 = new MWCharArray(tsDataJSON)   ; // Array of Input to be sent to MATLAB Runtime
     calibration = new Calibration();
     result = calibration.calibrationAPI(1, number1);                    
  }
  catch (Exception e)
  {
     e.printStackTrace();
  }
  finally
  {
     MWArray.disposeArray(number1);
     MWArray.disposeArray(result);
     calibration.dispose();
  }

  if(result.length >= 1){
      return result[0].toString();
  }

  return null;
}

Exception

Exception in thread "main" java.lang.IllegalStateException: Attempt to use an MWArray that has been disposed
    at com.mathworks.toolbox.javabuilder.NativeArray.get(NativeArray.java:122)
    at com.mathworks.toolbox.javabuilder.internal.MWMCR.mxArrayToString(Native Method)
    at com.mathworks.toolbox.javabuilder.internal.MWMCR.access$1600(MWMCR.java:31)
    at com.mathworks.toolbox.javabuilder.internal.MWMCR$6.mxArrayToString(MWMCR.java:949)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.mathworks.toolbox.javabuilder.internal.MWMCR$5.invoke(MWMCR.java:769)
    at com.sun.proxy.$Proxy0.mxArrayToString(Unknown Source)
    at com.mathworks.toolbox.javabuilder.MWBuiltinArray.toString(MWBuiltinArray.java:180)
    at com.mathworks.toolbox.javabuilder.MWCharArray.toString(MWCharArray.java:24)
    at .CHIndexCalculation.getCHINdex(CHIndexCalculation.java:45)
    at .CHIndexCalculation.main(CHIndexCalculation.java:15)

I am not sure whats causing this exception ? This happens when I run the code for say second time. From the error I can figure out that it has to do something with using a disposed array. But I am not sure why this comes up. I create a new object everytime.


Solution

  • I have figured it out. I just had to move the last if block in try. I was trying to extract result from an already disposed MWArray.

    public String getCHINdex(String tsDataJSON){
              MWCharArray number1 = null;
              Object[] result = null;
              Calibration calibration = null;
    
              try
              {
                 number1 = new MWCharArray(tsDataJSON)   ; // Array of Input to be sent to MATLAB Runtime
                 calibration = new Calibration();
                 result = calibration.calibrationAPI(1, number1);
    
                 if(result.length >= 1){
                      return result[0].toString();
                 }
              }
              catch (Exception e)
              {
                 e.printStackTrace();
              }
              finally
              {
                 MWArray.disposeArray(number1);
                 MWArray.disposeArray(result);
                 calibration.dispose();
              }
    
              return null;
    
        }