为什么我在jna中扩展Structure的类构造函数中获取NullPointerException?

时间:2014-03-26 14:03:44

标签: java nullpointerexception jna

我是JNA(Java Native Access)的新手。我想使用它的Structure类,但是当我在StructureTest类中使用newInstance(Class)方法实现扩展Structure的类(test)时,它会在测试类的构造函数上抛出NullPointerException。

这是StructureTest类的代码,我在其中实例化测试类。所有这些代码都在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();
        }

    }
}

这是测试类的代码,存储在test.java中,在其构造函数中我得到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);
    }

} 

这是完整的痕迹..

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)

2 个答案:

答案 0 :(得分:2)

我还没有使用JNA,但是查看getFieldOrder()方法文档,看起来您的问题出现在以下行 -

return Arrays.asList(name, roll);

将值"Shubham" and "33"作为字符串返回。它应该是字段名称"name" and "roll",作为字符串 -

return Arrays.asList("name", "roll");

答案 1 :(得分:0)

试试这个,我认为它应该有用,我没有运行,但这是我的猜测。

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

在主

obj.inner_obj.writeField(...);

试试这个