简体   繁体   English

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

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

I am new to JNA (Java Native Access). 我是JNA(Java本机访问)的新手。 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. 我想使用其Structure类,但是当我在StructureTest类中使用newInstance(Class)方法实例化扩展Structure的类(测试)时,它将在测试类的构造函数上抛出NullPointerException。

This is the code for the StructureTest class, where I instantiate the test class. 这是StructureTest类的代码,我在其中实例化测试类。 All this code is in StructureTest.java. 所有这些代码都在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. 这是测试类的代码,存储在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);
    }

} 

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 - 我没有使用过JNA,但查看getFieldOrder()方法文档,似乎您的问题在以下行中-

return Arrays.asList(name, roll);

That returns the values, "Shubham" and "33" , as strings. 这将返回值"Shubham" and "33"作为字符串。 It's supposed to be the field names, "name" and "roll" , as strings - 应该是字段名称"name" and "roll" ,为字符串-

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 尝试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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