1

I am new to JNA (Java Native Access). I wanted to work with its Structure class, but when I instanstiate the class (test), which extends Structure, using the newInstance(Class) method, in the StructureTest class, it throws a NullPointerException on the test class' constructor.

This is the code for the StructureTest class, where I instantiate the test class. All this code is in StructureTest.java.

package jna;
import java.util.List;

import com.sun.jna.*;
public class StructureTest {


    List<?> ff;
    Structure fild;
    test obj=new test();
    StructureTest() throws IllegalArgumentException, IllegalAccessException, Clas   sNotFoundException
    {
           fild=Structure.newInstance(obj.getClass());

    }

    public static void main(String[] args) {

        try {
            StructureTest obj=new StructureTest();
            obj.fild.writeField("name", "Grover");
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

This is the code for the test class, stored in test.java, on whose constructor I'm getting NullPointerException.

package jna;

import java.util.Arrays;
import java.util.List;

import com.sun.jna.Structure;

public class test extends Structure{
    public String name;
    public String roll;

    public test() {

        name=new String("Shubham");
        roll=new String("33");
    }


    public static void main(String args)
    {
        test obj=new test();
    }


    @Override
    protected List<String> getFieldOrder() {

        // TODO Auto-generated method stub
         return Arrays.asList(name,roll);
    }

} 

Here is the full trace..

Exception in thread "main" java.lang.NullPointerException
    at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.ComparableTimSort.sort(Unknown Source)
    at java.util.ComparableTimSort.sort(Unknown Source) 
    at java.util.Arrays.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at com.sun.jna.Structure.sort(Structure.java:893)
    at com.sun.jna.Structure.getFields(Structure.java:925)
    at com.sun.jna.Structure.deriveLayout(Structure.java:1058)
    at com.sun.jna.Structure.calculateSize(Structure.java:982) 
    at com.sun.jna.Structure.calculateSize(Structure.java:949)
    at com.sun.jna.Structure.allocateMemory(Structure.java:375)
    at com.sun.jna.Structure.<init>(Structure.java:184)
    at com.sun.jna.Structure.<init>(Structure.java:172)
    at com.sun.jna.Structure.<init>(Structure.java:159)
    at com.sun.jna.Structure.<init>(Structure.java:151)
    at jna.test.<init>(test.java:12)
    at jna.StructureTest.<init>(StructureTest.java:10)
    at jna.StructureTest.main(StructureTest.java:20)
5
  • please show us the full trace
    – MemLeak
    Mar 26, 2014 at 14:14
  • I tried your code but it ran without any issues (Java 7 with JNA 3.0.9).
    – rlegendi
    Mar 26, 2014 at 14:18
  • my code gives me NullPointerException on test.java constructor. I have added the full trace. Mar 26, 2014 at 14:20
  • You dont need fild, obj is already a structure. Also, don't use lowercase for class name
    – smttsp
    Mar 26, 2014 at 14:22
  • ok.. but my ques is why I m getting the NullPointerException on test.java constructor. Mar 26, 2014 at 14:27

2 Answers 2

2

I haven't used JNA but looking at the getFieldOrder() method docs, seems like your problem is at the following line -

return Arrays.asList(name, roll);

That returns the values, "Shubham" and "33", as strings. It's supposed to be the field names, "name" and "roll", as strings -

return Arrays.asList("name", "roll");
3
  • hmm.. this was the problem.. I am not getting the Exception now.. But can you plz tell me why this problem was coming?? Mar 26, 2014 at 14:38
  • Look at the stacktrace starting from bottom going upwards, the sequence is that in the constructor of Structure class your method is called, it looks for the Fields with names you return, but since there are no such fields, all it gets is null. Then it tries to sort the collection of those Fields, which are all null. The sort method internally tries to invoke the compareTo method on the null elements and then you get that NPE. Mar 26, 2014 at 14:43
  • Hmm.. a big thanx to u sir.. :) Mar 26, 2014 at 14:46
0

Try this, i think it should work, I didn't run but this is my guess.

test inner_obj;
StructureTest() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException
{
     new test();
}

inside main

obj.inner_obj.writeField(...);

Try this

1
  • Sir i have tried it..but failed.. It always gives NullPointerException Mar 26, 2014 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.