简体   繁体   中英

Why i m getting the NullPointerException in a class constructor extending Structure in jna?

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)

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");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM