Solved

ObjectArray in JNI

Posted on 2004-10-07
7
698 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now