JNA: Set struct pointer to NULL

Viewed 1846

I am starting to use JNA to communicate with a device on the RS485 interface of a computer. Suprisingly to me I came to good results very quickly. But now I am stuck by a simple problem. The library I use accepts a pointer to a pointer of struct. The actual signature is

func(Struct1 **, Struct2 **, Struct3 *, Struct4 *, long)

Now to indicate the size of the first parameter the library expects the last pointer to be a NULL pointer. This is what fails. Following code is what I tried so far:

    Struct1.ByReference[] s = (Struct1.ByReference[]) new Struct1.ByReference().toArray(size);
    int pos = 0;

    // ...
    // for loop to set the s[pos] struture values
    for(pos = 0; pos < size - 1; pos++)
    // ...

    // Now set the last array element to a null pointer to indicate end-of-list
    s[pos].getPointer().setPointer(0, null);// Following does not work: results in zero memoried structure
    s[pos] = null; // Following does not work wither: NullPointerException at com.sun.jna.Structure.autoWrite 

EDIT 1

s[pos] = new Struct1.ByReference(Pointer.NULL); // results in zero memoried structure as well

EDIT 2

According to technomage's question. If I were to write C code it would probably look something like that:

Struct1 **s = malloc(n * sizeof(Struct1*));

for(int i=0; i<n; i++)
{  
   if(i == n -1)
   {
      s[i] = NULL;
   }
   else
   {
      s[i] = malloc(sizeof(Struct1));
      s[i].bla = value;
      ....
   }
}

But be warned: I am not very skilled in C/C++. I consider Java to be my domain.

Has anyone had a similar problem? Maybe I am just not seeing the wood for the trees...

Thanks in advance.

1 Answers
Related