Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JNI GetMethodID Fails

Posted on 2004-03-26
4
Medium Priority
?
3,997 Views
Last Modified: 2012-05-04
I am attempting to get the method ID from a Java class using JNI.  I have recently starting having trouble with one of my classes.  I have created a wrapper class and helper functions to interface to the Java side and the functions work correctly in other locations.  Here is the relevent code:

jmethodID GetMethodID(JNIEnv * env, jclass jClass, LPCSTR sMethod, LPCSTR sPrototype)
{
   jmethodID mID;

   if (jClass == 0)
   {
      CString buf;
      buf.Format("ERROR! Unable to get method identifier for %s because the class is NULL.", sMethod);
      ReportError(buf, LOG_ERROR);
      return 0;
   }

   mID = (env)->GetMethodID(jClass, sMethod, sPrototype);

   if (mID == 0)
   {
      CString buf;
      buf.Format("ERROR! Unable to get method identifier for %s", sMethod);
      ReportError(buf, LOG_ERROR);
   }

   return mID;
}

.
.
.
if ( (m_Class  =  GetClass(m_pEnv, "com/bge/cda/common/CdaUser")) == 0)
{
   CString buf;
   buf.Format("ERROR! Unable to get jclass object of %s in JavaObject constructor.", sClass);
   ReportError(buf, LOG_ERROR);
}

jmethodID getUserName= GetMethodID(m_pEnv, m_Class, "getUserName", "()Ljava/lang/String;");
.
.
.

The output shows the following:

LEVEL 90        ERROR! Unable to get method identifier for isValid
LEVEL 90        ERROR! Unable to get method identifier for setValid
LEVEL 90        ERROR! Unable to get method identifier for setInvalid
LEVEL 90        ERROR! Unable to get method identifier for setUserName
LEVEL 90        ERROR! Unable to get method identifier for getUserName
LEVEL 90        ERROR! Unable to get method identifier for setUserId
LEVEL 90        ERROR! Unable to get method identifier for getUserId
LEVEL 90        ERROR! Unable to get method identifier for setFacility
LEVEL 90        ERROR! Unable to get method identifier for getFacility
LEVEL 90        ERROR! Unable to get method identifier for setSecurityGroup
LEVEL 90        ERROR! Unable to get method identifier for getSecurityGroup

Other classes using the same code work fine.  I just can't figure out why all of the calls to get the method ID fail when the object is valid.

Thanks!
0
Comment
Question by:icestac
  • 2
4 Comments
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 10691010
I think your class object is valid but it may not be the class object which has the listed method definitions..How you are getting the class object before passing to your GetMethodID method, Are you using GetObjectClass method?

-Lakshman
0
 
LVL 3

Author Comment

by:icestac
ID: 10691197
No, I am using the FindClass.  I guess I should have added that "GetClass()" is defined as follows:

jclass GetClass(JNIEnv * env, LPCSTR sClass)
{
   jclass cTemp;
   cTemp = env->FindClass(sClass);

   //Error check
   if (cTemp == 0)
   {
      CString buf;
      buf.Format("ERROR! Unable to create jclass object for the class %s", sClass);
      ReportError(buf, LOG_ERROR);
   }

   return cTemp;
}

I am creating the jclass object in the code above the call to get the method ID.  I do not think a jobject is required to get the method IDs, but rather a jclass.  I use the wrapper class later to invoke the methods on jobects using the jmethodIDs I am finding in this code.
0
 
LVL 3

Author Comment

by:icestac
ID: 10691921
Found the answer... Hopefully someone else can use this in the future:

If you use an incorrect function signature, all calls made to GetMethodID after that will fail even if they ARE correct.  In my case, the first function that I added was imported with an old signature file produced by javas.
0
 

Accepted Solution

by:
DigitalXtreme earned 0 total points
ID: 10692880
PAQ'ed and points refunded as requested in: http:/Community_Support/Q_20933986.html

DigitalXtreme
Community Support Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question