Solved

JNI GetMethodID Fails

Posted on 2004-03-26
4
3,978 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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