Solved

JNI GetMethodID Fails

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

803 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