Solved

JNI GetMethodID Fails

Posted on 2004-03-26
4
3,972 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now