external_objects_java: fix crash on ScilabJavaEnvironment when used as a local variable
[scilab.git] / scilab / modules / external_objects_java / sci_gateway / cpp / sci_jallowClassReloading.cpp
index edd0976..a8635fb 100644 (file)
  *
  */
 
-#include "ScilabJavaEnvironment.hxx"
-#include "ScilabGateway.hxx"
-
 extern "C" {
 #include "Scierror.h"
 #include "gw_external_objects_java.h"
 }
 
+#include "ScilabJavaEnvironment.hxx"
+#include "ScilabGateway.hxx"
+
 using namespace org_scilab_modules_external_objects_java;
 using namespace org_modules_external_objects;
 
@@ -27,7 +27,15 @@ int sci_jallowClassReloading(char * fname, unsigned long fname_len)
     try
     {
         const int envId = ScilabJavaEnvironment::start();
-        JavaOptionsSetter setter = ScilabJavaEnvironment::getInstance().getOptionsHelper().getSetter(JavaOptionsSetter::ALLOWRELOAD);
+
+        ScilabJavaEnvironment *javaEnvironment = ScilabJavaEnvironment::getInstance();
+        if (!javaEnvironment)
+        {
+            Scierror(999, "%s: No Java environment available (instance is null).", fname);
+            return 0;
+        }
+
+        JavaOptionsSetter setter = javaEnvironment->getOptionsHelper().getSetter(JavaOptionsSetter::ALLOWRELOAD);
         return ScilabGateway::getsetOptions(fname, envId, setter, pvApiCtx);
     }
     catch (std::exception & e)