Solved

ObjectArray in JNI

Posted on 2004-10-07
7
712 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
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.

 

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

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

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

729 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