java.lang.IllegalArgumentException - Structure array elements must use contiguous memory (bad backing address at Structure array index 1)

1,236 views
Skip to first unread message

naidu

unread,
Dec 19, 2011, 10:51:10 AM12/19/11
to jna-...@googlegroups.com

Hi, I'm getting the above exception when I tried to call a method using JNA.

The C function needs to be passed a TermStruc array.  

I tried to use Java like following :

XPoint[] cp1 = new XPoint[2];
cp1[0] = new XPoint();
cp1[0].a = 456.45;
cp1[0].b = 456.45;
cp1[1] = new XPoint();
..............
Then I called it like --> mylib.Eabc(XPoint[] xp).. How do I fix the error. When I use XPoint.ByReference[] it's giving me wrong error message from 3rd party DLL that the input is bad :(
 

Original C method -> Eabc (TERMSTRUC *)

 I defined equivalent methof like this --> it takes the current parameter like ABC(XPoint[] cp,
 --------------------------------------------------
#define XPOINT
typedef struct
{
  FEA_FLT    tenor;
  FEA_FLT    rate;
} XPOINT;
#endif


public class XPoint extends Structure {
 
 public double tenor;
 public double rate;


public static class ByReference extends XPoint implements Structure.ByReference {
 
 };

 .... }

 

 

Timothy Wall

unread,
Dec 19, 2011, 11:27:49 AM12/19/11
to jna-...@googlegroups.com
Use the Structure.toArray() method to obtain the structure array.

XPoint[] array = (XPoint[])new XPoint().toArray(2);

naidu

unread,
Dec 20, 2011, 6:58:23 AM12/20/11
to Java Native Access
That worked great. Thanks. :) Can you please help me understand how to
make arrays of structs and primitives as pass by reference? I think my
data structure is wrong. It's structure is like below :

typedef struct {
double * abc;
TERMCURVE * h ;
} OUTPUT_STRU;

How should my equivalent Java definition be for Pass By Reference? I
looked at a VB application that's using it and it has defined delta as
an array of double and termcurve also as an array. I need BOTH of them
by reference. Please help. I defined the structure like below :

public class OutputStru extends Structure {
public static class ByReference extends FeaOutput implements
Structure.ByReference {}
public double[] abc;
public TermCurve[] h; // will this work if we use
termCurve.ByReference[] ?

Timothy Wall

unread,
Dec 20, 2011, 11:09:37 AM12/20/11
to jna-...@googlegroups.com
Please refer to the online documentation for struct/union usage.
https://github.com/twall/jna/blob/master/www/StructuresAndUnions.md
https://github.com/twall/jna/blob/master/www/FrequentlyAskedQuestions.md

In general, within a Structure define pointer types with type Pointer until you need something more specific.

In your case below, if Java is providing the data, then the "abc" field must be either com.sun.jna.Memory or an NIO buffer.
If the field "h" is a pointer to an array of structures, then you should first create an appropriate array of structures (using Structure.toArray()), then assign the address of the first element of that array to "h" (either with Structure.getPointer() or by converting the first element into an instance of Structure.ByReference).

If "h" is of type Pointer, the underlying structure will *not* be automatically synched with native memory before/after the call; if it is of type Structure.ByReference, then the structure (and any associated array) will be synched automatically.

Reply all
Reply to author
Forward
0 new messages