1

I intend to convert my MATLAB code into java. I am following this official documentation. But after compiling using javac I am unable to complete the step 17. When I run this from cmd java -classpath .;"C:\Program Files\MATLAB\MATLAB Runtime\v93\toolbox\javabuilder\jar\javabuilder.jar";makesqr.jar getmagic 5

I get the error: Exception in thread "main" java.lang.NullPointerException at getmagic.main(getmagic.java:36)

I am simply following the tutorial for now and do not understand what is happening inside the file getmagic.java.

    import com.mathworks.toolbox.javabuilder.*;
import makesqr.*;

class getmagic
{
   public static void main(String[] args)
   {
      MWNumericArray n = null;
      Object[] result = null;
      Class1 theMagic = null;

      if (args.length == 0)
      {
        System.out.println("Error: must input a positive integer");
        return;
      }

      try
      {
         n = new MWNumericArray(Double.valueOf(args[0]),
                                      MWClassID.DOUBLE);

         theMagic = new Class1();

         result = theMagic.makesqr(1, n);
         System.out.println(result[0]);
      }
      catch (Exception e)
      {
         System.out.println("Exception: " + e.toString());
      }
      finally
      {
         MWArray.disposeArray(n);
         MWArray.disposeArray(result);
         theMagic.dispose();
      }
   }
}

Error is at line 36: theMagic.dispose();

kaqqao
  • 12,984
  • 10
  • 64
  • 118
Arsalan Ahmed
  • 383
  • 2
  • 3
  • 14
  • After the edit, this is a completely different question. It's now about the missing/incompatible dll. So open a new question and describe your new situation. – kaqqao Feb 06 '18 at 13:00

2 Answers2

1

why are you putting the dispose in the finaly brackets? Everything inside the final braces gets executed even if you get an exception in the try block

So in your case, you first set theMagic to null, this value only gets set in your try block, and only if no errors occur. That way, as soon as an error occurs, theMagic is not getting set but you still try to call theMagic.dispose on an null Value.

I guess the simplest Solution would be to write your dispose calls inside the try block.

Daschifahrer
  • 40
  • 1
  • 6
0

before dispose , check if is not null :

if(theMagic != null){
    theMagic.dispose();
}
Albanninou
  • 409
  • 2
  • 11