Solved

ObjectArray in JNI

Posted on 2004-10-07
7
711 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
[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
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jmss example java 2 48
batch vs regular insert in spring DAO 2 50
JDBC performance 8 33
using executorService 3 18
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

737 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