【问题标题】:NullPointerException while using Array of objects in java在 java 中使用对象数组时出现 NullPointerException
【发布时间】:2011-05-04 21:59:46
【问题描述】:

我有这段代码在注释行抛出空指针异常。我认为这可能是因为我没有初始化数组 ParentInterfaces 对吗??...问题是因为这个数组可能包含从 2 到 30 的许多元素我不喜欢在构造函数中初始化它的想法如果我这样做不会导致 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]; 

父接口计数++; } } 现在导致调用此函数的代码是:(它属于 diff 类中的 diff 函数)

    // Prev Code    
    else if (child.getNodeName()=="ParentInterface")
    {
        Iflag=1;
        //Element ele=(Element)child;
        JOptionPane.showMessageDialog (null, "Parent Interface found" + child.getTextContent () + "for " + classes[i].name);
        parentInterfaces[parentICount] = new dataObjects.Interface (child.getTextContent ());
        // classes[i].parentInterfaces[parentICount] = new dataObjects.Interface (child.getTextContent ());
        parentICount++;
    }
}
classes[i].setParentInterfaces (parentInterfaces);

【问题讨论】:

  • 你不能在 setParentInterfaces 中这样做吗:parentInterfaces = new Interface[interfaces.Length],在 while 循环之前
  • classes[i] 在哪里设置?
  • @CoolBeans:设置正确。在代码的早期部分..我已经验证了它。因为我可以正确访问其他属性,如 classes[i].name ...
  • @forsvarir: wow..that's a cool option to use..但我认为最好将所有数组更改为arraylist...

标签: java arrays memory-leaks nullpointerexception


【解决方案1】:

是的,这是因为你还没有初始化 parentInterfaces。

您的选择包括:

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

顺便说一句,您的 setParentInterfaces() 方法不起作用,因为数组索引没有增加。

【讨论】:

  • 是的,我同意....将尝试第二次或第三次...取决于需要进行多少重组工作
【解决方案2】:

您声明了 parentInterfaces 变量,但没有初始化它。试试这个....

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

    }

}

【讨论】:

  • 但是 while() 循环被破坏了 - parentInterfaceCount 没有被增加,所以循环要么永远执行,要么永远执行!
  • @DNA - 你没有错……但我把最初的问题是“为什么我会得到 NullPointerException”?我没有再看什么了。
【解决方案3】:

考虑使用ArrayList<Interface> 而不是数组,然后使用parentInterfaces.add(item) 添加项目并使用parentInterfaces.get() 检索它们。 ArrayLists 为您处理内存分配。

事实上,对所有变量使用 ArrayLists 而不是数组可能会更好。

【讨论】:

    【解决方案4】:

    您通常应该在使用数组之前对其进行初始化。另外,不要将您的课程命名为 Class。这已经存在于 java.lang 包中。

    【讨论】:

      【解决方案5】:

      你还没有初始化你的数组。你需要做这样的事情

      parentInterfaces = new Interface[];
      

      虽然我必须说这是一个有趣的代码。您不需要在 java 中的变量中维护数组计数。你可以说 arrayObject.length();

      我说要买一本好的java书:)

      【讨论】:

      • 好吧,我不知道它看起来有多好笑,但是 arrayObject.length 会给你数组的大小,即如果你做 new Interface[50],你会得到 50,而不管实际对象的数量是多少数组..在我的情况下不受欢迎..第二个您提供的解决方案甚至无法编译...n给出错误:变量必须提供维度表达式或数组初始化器......所以可能是你想再读一遍那本java书……:)
      【解决方案6】:

      问题是因为这个数组可能包含许多从 2 到 30 的元素,我不喜欢在构造函数中初始化它的想法,如果我这样做会不会导致 function2 中的内存泄漏,因为我有那里有一个对象,我将其引用传递给另一个函数????

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

      在您的代码上:

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

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

          while(interfaces[parentInterfaceCount]!=null)
          {
              parentInterfaces[parentInterfaceCount]=interfaces[parentInterfaceCount];
      
          }
      

      因为你不增加 parentInterfaceCount,这段代码要么什么都不做,要么进入一个无限循环。所以,我很难弄清楚你想要什么。 您收到空指针异常的原因是 parentInterfaces[parentInterfaceCount] 只有在初始化 parentInterfaces 时才能得到解决。

      如果我知道您想要完成什么,会更容易提供帮助。

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

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

      1. 使用内置数组复制方法,如System.arraycopy
      2. 使用 for 循环——它使处理诸如递增指针之类的事情变得更加容易
      3. 使用集合类

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-12
        • 2010-12-27
        • 1970-01-01
        • 2011-12-03
        相关资源
        最近更新 更多