Link to home
Start Free TrialLog in
Avatar of urmi123
urmi123

asked on

ObjectArray in JNI

Hi,
 I am having two classes in java. I am trying to call one class in other class like we write structures in C.
say in a.java i have declared variables as string x; string y;
and in b.java i have variables as char lineIsSelected, String termNum, String      heading1, String heading2
But i have to make b.java as array.
so i called b.java in a.java as
b []temp;
I have a numberofmatrixlines on which the loop is formed.

i had wriiten the code as follows
      public void createUpdStruct(int size)
      {
            updStruct = new updCHSMatrixStruct[size];
      }

      public void setUpdStruct(int size, int index)
      {                  
                  updStruct[index].lineIsSelected = lineIsSelected;
                  updStruct[index].heading1 = heading1;
                  updStruct[index].heading2 = heading2;
                  updStruct[index].termNum = termNum;
      }                                                                                                                  

      public updCHSMatrixStruct getUpdStruct(int size)
      {
            return updStruct[index];
      }      

but when i call it on c side it fails .
can anyone help me with this.

Regds
urmilla

Avatar of nesnemis
nesnemis

what exactly fails?
I need more code and the error message
As nesmeis propsed: Post more code, esp your C code calling the Java method.

BTW:

     public updCHSMatrixStruct getUpdStruct(int size)
     {
          return updStruct[index];
     }

should be:

     public updCHSMatrixStruct getUpdStruct(int index)
     {
          return updStruct[index];
     }

Right?

GetObjectArrayElement returns the object element at a given index.
SetObjectArrayElement updates the object element at a given index.

this could be of some help to you
http://java.sun.com/docs/books/tutorial/native1.1/implementing/cpp.html
Avatar of urmi123

ASKER

Hi,
       public void createUpdStruct(int size)
      {
            updStruct = new updCHSMatrixStruct[size];
      }

      public void setUpdStruct(int index, char lineIsSelected, String termNum, String heading1, String heading2)
      {                  
            updStruct[index].lineIsSelected = lineIsSelected;
            updStruct[index].heading1 = heading1;
            updStruct[index].heading2 = heading2;
            updStruct[index].termNum = termNum;

      }                                                                                                                  

      public updCHSMatrixStruct getUpdStruct(int index)
      {
            return updStruct[index];
      }                                                                                                                  
This is the code i had wriiten in java.

And in c i had called it as follows

mid = (*env)->GetMethodID(env, cls, "createUpdStruct", "(I)V");      
{
   if (mid != NULL)
{
                  (*env)->CallVoidMethod(env, *jTGStruct, mid, (jint)TGStruct->numberOfMatrixLines);
            }
      }      
      
      mid = (*env)->GetMethodID(env, cls, "setUpdStruct", "(ICLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
      {
            if (mid != NULL)
            {            
                  for(index = 0; index < TGStruct->numberOfMatrixLines; index++)
                  {
                        jheading1      = getStringNative(env, TGStruct->updStruct[index].heading1, strlen(TGStruct->updStruct[index].heading1));
                        jheading2      = getStringNative(env, TGStruct->updStruct[index].heading2, strlen(TGStruct->updStruct[index].heading2));
                        jtermNum      = getStringNative(env, TGStruct->updStruct[index].termNum, strlen(TGStruct->updStruct[index].termNum));    
                        (*env)->CallVoidMethod(env, *jTGStruct, mid, (jint)index,(jchar)TGStruct->updStruct[index].lineIsSelected, jheading1, jheading2, jtermNum);      
                        (*env)->DeleteLocalRef(env, jheading1);
                        (*env)->DeleteLocalRef(env, jheading2);
                        (*env)->DeleteLocalRef(env, jtermNum);      
                  }
                                          
            }
      }
Avatar of urmi123

ASKER

Hi,
 Following is the java code
public void createUpdStruct(int size)
{
 updStruct = new updCHSMatrixStruct[size];
}

public void setUpdStruct(int index, char lineIsSelected, String termNum, String heading1, String heading2)
{                  
   updStruct[index].lineIsSelected = lineIsSelected;
   updStruct[index].heading1 = heading1;
   updStruct[index].heading2 = heading2;
   updStruct[index].termNum = termNum;
}
public updCHSMatrixStruct getUpdStruct(int index)
{
  return updStruct[index];
}                                                                                                                  
AN c code is as follows
mid = (*env)->GetMethodID(env, cls, "createUpdStruct", "(I)V");      
{
if (mid != NULL)
   {
     (*env)->CallVoidMethod(env, *jTGStruct, mid, (jint)TGStruct->numberOfMatrixLines);
    }
}      
      
mid = (*env)->GetMethodID(env, cls, "setUpdStruct", "(ICLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
{
  if (mid != NULL)
   {            
          for(index = 0; index < TGStruct->numberOfMatrixLines; index++)
          {
      jheading1      = getStringNative(env, TGStruct->updStruct[index].heading1, strlen(TGStruct->updStruct[index].heading1));
      jheading2      = getStringNative(env, TGStruct->updStruct[index].heading2, strlen(TGStruct->updStruct[index].heading2));
      jtermNum      = getStringNative(env, TGStruct->updStruct[index].termNum, strlen(TGStruct->updStruct[index].termNum));    
      (*env)->CallVoidMethod(env, *jTGStruct, mid, (jint)index,(jchar)TGStruct->updStruct[index].lineIsSelected, jheading1, jheading2, jtermNum);      
      (*env)->DeleteLocalRef(env, jheading1);
      (*env)->DeleteLocalRef(env, jheading2);
      (*env)->DeleteLocalRef(env, jtermNum);      
       }
                                          
     }
}

when index is set to 1 it fails .

Regds
urmilla
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
8-)