Solved

ObjectArray in JNI

Posted on 2004-10-07
7
702 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:urmi123
7 Comments
 
LVL 3

Expert Comment

by:nesnemis
ID: 12248448
what exactly fails?
I need more code and the error message
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 12248596
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?

0
 
LVL 6

Expert Comment

by:expertmb
ID: 12249935
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
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:urmi123
ID: 12256126
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);      
                  }
                                          
            }
      }
0
 

Author Comment

by:urmi123
ID: 12256162
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
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12256782
>>updStruct = new updCHSMatrixStruct[size];

You allocate memory for the above array, but where do you allocate memory for the elements of that array (by creating instances of the right type)?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12379943
8-)
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 59
fibonacci ten numbers 4 38
Unhandled exception type Exception 18 31
sql import cannot be resolved jsp 3 23
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 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