How to resolve a Bad global or local ref passed to JNI error when calling a java method from native code

vickee

i searched through many answers for this problem but none of the seemed to match my case.I could not figure out where i am making the mistake.I create a native method in java and call it through jni.In the native method i get the reference of a java class method and call it from there

My java class is as follows:

import java.util.ArrayList ; 
import java.util.LinkedHashMap ; 

public class WmiClientClassJson {

    static {
        System.load("C:\\Users\\vignesh-pt1616\\Desktop\\WmiClientLibraryJson\\Debug\\WmiClientLibraryJson.dll");
    }

    public native void createConnection(String query , LinkedHashMap<String,Object> map , ArrayList<LinkedHashMap<String,Object>> list);

    public static void main(String[] args)
    {
        WmiClientClassJson obj = new WmiClientClassJson(); 
        LinkedHashMap<String , Object> map = new LinkedHashMap<String , Object>(); 
        ArrayList<LinkedHashMap<String , Object>> list= new ArrayList<LinkedHashMap<String , Object>>();
        obj.createConnection("String" , map , list );
    }

    public void indexToEs()
    {
        System.out.println("index method called");
    }
}

I have generated the dll library which is placed in the specified directory.My native method implementation is as follows .

JNIEXPORT void JNICALL Java_WmiClientClassJson_createConnection(JNIEnv *env, jobject classobj , jstring query , jobject mmapobj , jobject listobj )
{

    //Getting ListClass reference
    jclass ListClass = env->GetObjectClass(listobj);
    if( ListClass == NULL )
    {
        cout << "ListClass null " << endl ; 
        return ;
    }

    jmethodID listConstructor = env->GetMethodID(ListClass,"<init>","()V");
    if(listConstructor == NULL)
    {
        cout << "mapconstructor null "<< endl ; 
        return ; 
    }
    cout<< "list constructor found" << endl ; 

    //Getting ArrayList add method 
    jmethodID addMethod = env->GetMethodID(ListClass , "add" , "(Ljava/lang/Object;)Z");
    if(addMethod == NULL)
    {
        cout << "addMethod null "<< endl ; 
        return ; 
    }
    //cout << "add method found " << endl ; 

    jmethodID sizeMethod = env->GetMethodID(ListClass , "size" , "()I");
    if(sizeMethod == NULL )
    {
        cout << "sizeMethod null" << endl ; 
        return ; 
    }
    cout << "size method found" << endl ; 

    jclass callingClass = env->GetObjectClass(classobj);
    if( callingClass = NULL )
    {
        cout << "Calling class null " << endl ; 
        return ; 
    }
    else cout << "calling class found" << endl ;

    jmethodID indexMethod = env->GetMethodID(callingClass , "indexToEs" , "()V"); // the code throws an error at this point
    if( indexMethod == NULL )
    {
        cout << "index method not found" << endl ;
        return ;  
    }
    else cout << "index method found" << endl ;
}

when i try to get the method id of the indexToEs method i am getting the Bad global or local ref passed to JNI error

WARNING: JNI local refs: zu, exceeds capacity: zu
        at java.lang.System.initProperties(Native Method)
        at java.lang.System.initializeSystemClass(Unknown Source)
WARNING in native method: JNI call made without checking exceptions when required to from CallStaticObjectMethod
list constructor found
size method found
calling class found
FATAL ERROR in native method: Bad global or local ref passed to JNI
        at WmiClientClassJson.createConnection(Native Method)
        at WmiClientClassJson.main(WmiClientClassJson.java:17)

please help me out with this thing.Thanks in advance.

Alex Cohn
if( callingClass = NULL )

will take the class reference and change it to 0.

You should pay more attention to compiler warnings, usually C++ compilers remind you to never write

if ( something = 0 )

The fix is to write

if( callingClass == NULL )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a Java Method from the native code using jni

From Dev

Calling a static void Java method from JNI

From Dev

Android Jni : crash in global and local ref variables

From Dev

SIGSEGV when calling Java method from native pthread

From Java

In JNI function when I change an Array in-place that was passed from Java code the Array doesn't modify

From Java

JNI calling JVM functions/events from native code?

From Dev

Returning local reference created by JNI from a native method

From Dev

Spark - UnsupportedOperationException when calling Java method from Scala code

From Dev

Bad Compile Error Calling Scala from Java

From Dev

Undefined ref when calling child method from parent

From Dev

JNI: Calling a java method from C periodically is not working

From Dev

Java JNI NullPointerException after calling method from C with valid pointers

From Dev

how to resolve Python regex error "error bad escape \m at position 37", when passing from 3.5.4 to 3.6.8

From Java

How to pass List from Java to JNI C++ as vector by ref?

From Java

How to resolve this JNI error when trying to run LWJGL "Hello World"?

From Java

A java runtime error with a native JNI static method for C++ implementation

From Dev

How to resolve Error external exception E0434352 on calling a C# dll method from Delphi

From Java

How to solve error with calling kotlin activity method from java singleton?

From Dev

JQuery calling global from local

From Dev

How is "value type" variable freed from the stack when leaving method scope if it is still in use by another thread (passed by "ref")?

From Dev

JNI : Unable to find java class from native method in a callback

From Dev

undefined local variable or method from &block when calling instance method

From Dev

Why does the Java compiler show an error when calling a function in main with an array[] parameter and how to resolve this issue?

From Dev

Identifier expected error when calling a method in java

From Dev

native GWT : calling java method from native js

From Dev

JNI C++ UnsatisfiedLinkError When Calling Method

From Dev

How to create a Java Library (API) with native code via JNI

From Dev

How to debug native jni c++ code in eclipse with java project

From Android

How to resolve android compile error 'cant find symbol' from react-native-code-push

Related Related

  1. 1

    Calling a Java Method from the native code using jni

  2. 2

    Calling a static void Java method from JNI

  3. 3

    Android Jni : crash in global and local ref variables

  4. 4

    SIGSEGV when calling Java method from native pthread

  5. 5

    In JNI function when I change an Array in-place that was passed from Java code the Array doesn't modify

  6. 6

    JNI calling JVM functions/events from native code?

  7. 7

    Returning local reference created by JNI from a native method

  8. 8

    Spark - UnsupportedOperationException when calling Java method from Scala code

  9. 9

    Bad Compile Error Calling Scala from Java

  10. 10

    Undefined ref when calling child method from parent

  11. 11

    JNI: Calling a java method from C periodically is not working

  12. 12

    Java JNI NullPointerException after calling method from C with valid pointers

  13. 13

    how to resolve Python regex error "error bad escape \m at position 37", when passing from 3.5.4 to 3.6.8

  14. 14

    How to pass List from Java to JNI C++ as vector by ref?

  15. 15

    How to resolve this JNI error when trying to run LWJGL "Hello World"?

  16. 16

    A java runtime error with a native JNI static method for C++ implementation

  17. 17

    How to resolve Error external exception E0434352 on calling a C# dll method from Delphi

  18. 18

    How to solve error with calling kotlin activity method from java singleton?

  19. 19

    JQuery calling global from local

  20. 20

    How is "value type" variable freed from the stack when leaving method scope if it is still in use by another thread (passed by "ref")?

  21. 21

    JNI : Unable to find java class from native method in a callback

  22. 22

    undefined local variable or method from &block when calling instance method

  23. 23

    Why does the Java compiler show an error when calling a function in main with an array[] parameter and how to resolve this issue?

  24. 24

    Identifier expected error when calling a method in java

  25. 25

    native GWT : calling java method from native js

  26. 26

    JNI C++ UnsatisfiedLinkError When Calling Method

  27. 27

    How to create a Java Library (API) with native code via JNI

  28. 28

    How to debug native jni c++ code in eclipse with java project

  29. 29

    How to resolve android compile error 'cant find symbol' from react-native-code-push

HotTag

Archive