Solved

JNI GetMethodID Fails

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 156
C++ question 3 84
sorting efficency of sorting algorithm 30 151
cmake and message 1 26
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to clear a vector as well as how to detect empty vectors in C++.

759 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