

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
1 message in net.java.dev.jna.usersProblem with Structure array member| From | Sent On | Attachments |
|---|---|---|
| Rob Cope | Sep 13, 2007 9:15 pm |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Problem with Structure array member | Actions... |
|---|---|---|
| From: | Rob Cope (ro...@eclipseservices.com) | |
| Date: | Sep 13, 2007 9:15:00 pm | |
| List: | net.java.dev.jna.users | |
I have two Structures defined thusly:
public static class SecKeychainAttribute extends Structure { public int tag; public int length; public String data; }
public static class SecKeychainAttributeList extends Structure { public int count; public SecKeychainAttribute[] attr; }
Then I defined a native Library with the following method:
int SecKeychainItemModifyAttributesAndData( Pointer itemRef, SecKeychainAttributeList attrList, int length, String pw);
When I try to call that method, I get the following NullPointerException (this is using the latest source in the svn trunk):
java.lang.NullPointerException at com.sun.jna.Structure.getNativeSize(Structure.java:712) at com.sun.jna.Structure.getNativeAlignment(Structure.java:651) at com.sun.jna.Structure.getNativeAlignment(Structure.java:668) at com.sun.jna.Structure.calculateSize(Structure.java:607) at com.sun.jna.Structure.allocateMemory(Structure.java:139) at com.sun.jna.Structure.write(Structure.java:374) at com.sun.jna.Function.convertArgument(Function.java:338) at com.sun.jna.Function.invoke(Function.java:177) at com.sun.jna.Library$Handler.invoke(Library.java:170) at $Proxy0.SecKeychainItemModifyAttributesAndData(Unknown Source) at keychaintest.Main.modifyPassword(Main.java:177) at keychaintest.Main.main(Main.java:198)
The relevant bits start in Structure.getNativeAlignment, line 668:
667 else if (type.isArray()) { 668 alignment = getNativeAlignment(type.getComponentType (), null, firstElement); 669 }
Note that null is passed as the value. Then getNativeAlignment line 651:
651 int size = getNativeSize(type, value);
So that is passing null as the value to getNativeSize. Finally, in Structure.getNativeSize, like 712:
708 protected int getNativeSize(Class type, Object value) { 709 if (Structure.class.isAssignableFrom(type)) { 710 Structure s = (Structure)value; 711 // inline structure 712 return s.size(); //There's the NPE 713 }
I'm not totally sure what to do about this, however. Checking for value != null in getNativeSize only throws an exception later in getNativeSize(Class cls), since the class in this case is a Structure.
I tried changing SecKeychainAttributeList to this:
public static class SecKeychainAttributeList extends Structure { public int count; public Pointer[] attr; }
and then setting it up like this:
SecKeychainAttribute attr = new SecKeychainAttribute(); ... SecKeychainAttributeList attrList = new SecKeychainAttributeList(); attrList.attr = new Pointer[1]; attrList.attr[0] = attrPtr; attrList.count = attrList.attr.length; attr.write(); int result = keychain.SecKeychainItemModifyAttributesAndData (itemRef, attrList, pw.length(), pw);
But then I just get this;
java.lang.IllegalArgumentException: Inline array of class com.sun.jna.Pointer not supported at com.sun.jna.Structure.writeField(Structure.java:496)
Bummer. So I tried this:
public static class SecKeychainAttributeList extends Structure { public int count; public Pointer attr; }
But that just crashes the VM (result 138 on Mac OS 10.4.9, Java 1.5.0_07-87). Any advice?
Thanks, Rob







