Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用对象数组时出现NullPointerException_Java_Arrays_Memory Leaks_Nullpointerexception - Fatal编程技术网

在java中使用对象数组时出现NullPointerException

在java中使用对象数组时出现NullPointerException,java,arrays,memory-leaks,nullpointerexception,Java,Arrays,Memory Leaks,Nullpointerexception,我有一段代码,它在注释行抛出一个空指针异常。。我认为这可能是因为我没有初始化数组ParentInterfaces,对吗??…问题是,因为这个数组可能包含2到30个元素,所以我不希望在构造函数2中初始化它,如果我这样做,不会导致function2内存泄漏,因为我有一个我正在传递给另一个函数的引用???…是否有其他可用的替代方法 public class Class { public String modifier; public String name; public C

我有一段代码,它在注释行抛出一个空指针异常。。我认为这可能是因为我没有初始化数组ParentInterfaces,对吗??…问题是,因为这个数组可能包含2到30个元素,所以我不希望在构造函数2中初始化它,如果我这样做,不会导致function2内存泄漏,因为我有一个我正在传递给另一个函数的引用???…是否有其他可用的替代方法

public class Class {
    public String modifier;
    public String name;
    public  Class parent;
    public  Interface[] parentInterfaces;
    public Method[] memberFunctions;
    public Class[] nestedClasses;
    public Interface[] nestedInterfaces;
    public Field[] memberData;
    public int methodCount, classCount, interfaceCount, fieldCount, parentInterfaceCount;

public Class (String classModifier,String className)
{
    modifier=classModifier;
    name=className;
    methodCount=0;
    classCount=0;
    interfaceCount=0;
    fieldCount=0;
    parentInterfaceCount=0;     
}

public void setParentInterfaces (Interface[] interfaces)
{
    while (interfaces[parentInterfaceCount] != null)
    {
// This is the line which throws the NPE
        parentInterfaces [parentInterfaceCount] = interfaces [parentInterfaceCount]; 
parentINterfaceCount++; } } 现在导致调用该函数的代码是:(它属于diff类中的diff函数)


考虑使用
ArrayList
而不是数组,然后使用
parentInterfaces添加项。添加(项)
并使用
parentInterfaces.get()检索它们。ArrayList负责为您分配内存


事实上,最好对所有变量使用
ArrayLists
而不是数组。

考虑使用
ArrayList
而不是数组,然后使用
parentInterfaces添加项。添加(项)
并使用
parentInterfaces.get()
检索它们。ArrayList负责为您分配内存


事实上,对于所有变量,最好使用
ArrayLists
而不是数组。

通常应在使用数组之前初始化数组。另外,不要给你的班级命名。java.lang包中已经存在的数组。

您通常应该在使用数组之前初始化它们。另外,不要给你的班级命名。已经存在于java.lang包中。

您尚未初始化数组。你需要这样做

parentInterfaces = new Interface[];
虽然我必须说这是一个有趣的代码。在java中,不需要在变量中维护arraycounts。您可以说arrayObject.length()


我说,拿起一本好的java书:)

您还没有初始化数组。你需要这样做

parentInterfaces = new Interface[];
虽然我必须说这是一个有趣的代码。在java中,不需要在变量中维护arraycounts。您可以说arrayObject.length()


我说,拿起一本好的java书:)

声明parentInterfaces变量,但不初始化它。试试这个

public void setParentInterfaces(Interface[] interfaces)
{
    parentInterfaces = new Interface[interfaces.length]
    while(interfaces[parentInterfaceCount]!=null)
    {
        parentInterfaces[parentInterfaceCount]=interfaces[parentInterfaceCount]

    }

}

声明parentInterfaces变量,但不初始化它。试试这个

public void setParentInterfaces(Interface[] interfaces)
{
    parentInterfaces = new Interface[interfaces.length]
    while(interfaces[parentInterfaceCount]!=null)
    {
        parentInterfaces[parentInterfaceCount]=interfaces[parentInterfaceCount]

    }

}

是的,这是因为您尚未初始化ParentInterface

你的选择包括:

  • 使用预期的最大大小(30)初始化阵列
  • 我认为,使用可调整大小的集合(如ArrayList)需要对当前代码进行一些重构
  • 使用System.arraycopy将接口数组复制到parentInterfaces数组中

  • 顺便说一句,setParentInterfaces()方法无法工作,因为数组索引没有增加。

    是的,这是因为您没有初始化parentInterfaces

    你的选择包括:

  • 使用预期的最大大小(30)初始化阵列
  • 我认为,使用可调整大小的集合(如ArrayList)需要对当前代码进行一些重构
  • 使用System.arraycopy将接口数组复制到parentInterfaces数组中
  • 顺便说一句,setParentInterfaces()方法将无法工作,因为数组索引没有增加

    问题是,因为这个数组可能包含从2到30个元素,我不希望在构造函数第二次初始化它,如果我这样做,它不会导致function2中的内存泄漏,因为我有一个对象,它的引用正在传递给另一个函数

    简单回答:不。初始化数组不会创建任何对象,只是一个空指针数组。即使是这样,Java也让担心内存“泄漏”成为一个坏主意。另一方面,担心内存使用是值得的。因为它是一种垃圾收集语言,所以要问的问题是:垃圾收集器能让我的内存占用保持很小吗?垃圾收集器利用率的主要规则是:不要在堆上保留指向周围内容的额外指针。从内存的角度来看,将堆上的指针设置为null几乎总是可以的。堆栈上的指针几乎总是可以使用的

    在您的代码上:

    您的代码很难阅读,因为1。完全不清楚应该做什么(这是编译器的一部分吗?)。您的命名方案使用反射3中使用的类的语言/名称中的关键字。这里有相当多的代码,一般来说,最好是分叉巡更代码,并开始简化它,直到生成错误的绝对最小的代码

    特别是,我不知道这段代码应该做什么:

        while(interfaces[parentInterfaceCount]!=null)
        {
            parentInterfaces[parentInterfaceCount]=interfaces[parentInterfaceCount];
    
        }
    
    因为您不增加parentInterfaceCount,所以此代码要么不执行任何操作,要么将进入无限循环。所以,我很难弄明白你想要什么。 出现空指针异常的原因是,只有在初始化了
    parentInterfaces
    时才能处理
    parentInterfaces[parentInterfaceCount]

    如果我知道你想要完成什么,我会更容易帮助你

    也就是说,我认为你有两个选择: 将数组初始化为预期的最大大小,或 使用传递给您的数组的长度来计算运行时要初始化的大小

    让生活更简单的一些想法:

  • 使用内置的数组复制方法,如
    System.arraycopy
  • 使用for循环——它使处理诸如递增指针之类的事情变得更加容易
  • 使用集合类
  • 问题是,由于这个数组可能包含从2到30个元素,我不希望在构造函数2中初始化它,如果这样做,不会导致function2内存泄漏,因为我有一个引用