JNI: Help on using NewObject with constructors!

Discussion in 'Java' started by EagerToKnow, Feb 26, 2006.

  1. EagerToKnow

    EagerToKnow Guest

    Hi,All:

    I want to create an object in a native method. The object is an
    instance of an inner class. The inner class has two constructors: one
    default and one with two arguments. There is no problem if I use
    NewObject with the default construtor. But if I use NewObject with the
    constructor taking two int arguments. The JVM crashes.

    Can anybody give me some idea what the problem is?

    Thanks!

    Tao

    Here is the native method, the inner class and error message:

    JNIEXPORT jobject JNICALL Java_Prompt_createtp(JNIEnv *env, jobject
    obj)
    {
    jobject test_ttt= 0;
    jclass cls2;
    jmethodID cid;
    jint a,b;
    jfieldID fid;

    a = 10; b = 20;

    cls2 = (*env)->FindClass(env,"LPrompt$tp;");
    cid = (*env)->GetMethodID(env,cls2,"<init>","(LPrompt;II)V");
    test_ttt = (*env)->NewObject(env,cls2,cid,a,b);

    // However, the following codes using default constructor works well
    //cid = (*env)->GetMethodID(env,cls2,"<init>","(LPrompt;)V");
    //test_ttt = (*env)->NewObject(env,cls2,cid);

    (*env)->DeleteLocalRef(env,cls2);
    return test_ttt;

    }

    // here is the inner class
    class tp{

    int a;
    int b;

    tp(){
    a = 100;
    b = 0;
    }

    tp(int x,int y){
    a = x; b = y;
    }

    }

    // here is the crash message
    #
    # An unexpected error has been detected by HotSpot Virtual Machine:
    #
    # Internal Error (53484152454432554E54494D450E43505001A3), pid=276,
    tid=640
    #
    # Java VM: Java HotSpot(TM) Client VM (1.5.0_06-b05 mixed mode)
    # An error report file with more information is saved as
    hs_err_pid276.log
    #
    # If you would like to submit a bug report, please visit:
    # http://java.sun.com/webapps/bugreport/crash.jsp
    #
    EagerToKnow, Feb 26, 2006
    #1
    1. Advertising

  2. EagerToKnow

    Chris Uppal Guest

    Re: Help on using NewObject with constructors!

    EagerToKnow wrote:

    > I want to create an object in a native method. The object is an
    > instance of an inner class. The inner class has two constructors: one
    > default and one with two arguments. There is no problem if I use
    > NewObject with the default construtor. But if I use NewObject with the
    > constructor taking two int arguments. The JVM crashes.


    I can't see anything wrong with the code you posted. I may be missing
    something, but if not then that suggests that your problem may be elsewhere.
    Perhaps a rogue pointer or a stack corruption that causes a later crash which,
    by coincidence, happens when you call the constructor.

    -- chris
    Chris Uppal, Feb 26, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,279
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    588
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    596
    Ron Natalie
    Jun 7, 2007
  4. zhou820123

    NewObject error for JNI inner class

    zhou820123, Nov 3, 2010, in forum: Java
    Replies:
    1
    Views:
    1,214
    zhou820123
    Nov 3, 2010
  5. k0t1k
    Replies:
    2
    Views:
    92
    k0t1k
    Jun 28, 2005
Loading...

Share This Page