繁体   English   中英

Java中的空指针错误?

[英]Null-pointer error in Java?

我对这个论坛话题还很陌生。 我一直都很担心在论坛上发帖,但是经过两周的反复抨击我的代码以使其正常工作,我终于给出了建议。下面的代码是一些较大代码的关键部分,但我认为ve指出了问题部分。 我有一个2D对象数组,应该在创建对象后默认情况下对其进行初始化。 但是,这似乎没有发生,并且该值保持为空。 这可能是Java无法通过引用传递的结果吗? 如果是这样,为什么原始2D数组被完美实例化呢? 预先感谢您,拉斯。

    class ObjectThing
    {
        int someInt;
        double someDouble;

        public ObjectThing()
        {
            someInt = 0;
            someDouble = 0;
        }

        synchronized private void someIntIncrement ()
        {
           someInt++;
        }

        synchronized public void addSomeDouble (double sd)
        {
            someDouble += sd;
            someIntIncrement ();
        }

        synchronized public String toString ()
        {
            return someInt + "," + someDouble;
        }
  }


  class AnotherObject
  {
          String name;
          ObjectThing [][] someObjectMAtrix;
          double [][] someDoubleMAtrix01;
          double [][] someDoubleMAtrix02;   

          public AnotherObject()
          {
          }

          private void initDouble (double [][] mat)
          {
             for (double [] i: mat)
                for (double j: i)
                   j = 0;
          }

          private void initObject (ObjectThing [][] so)
          {
              for (ObjectThing [] i: so)
                 for (ObjectThing j: i)
                    j = new ObjectThing ();
          }         

          public void init (int r, int c)
          {
              someObjectMAtrix =  new ObjectThing [r][c];
              someDoubleMAtrix01 = new double [r][c];
              someDoubleMAtrix01 = new double [r][c];         
          initObject (someObjectMAtrix);
              initDouble (someDoubleMAtrix01);
              initDouble (someDoubleMAtrix02);
          }
   }

   class Driver
   {
       public static void main (String [] args)
       {
            initializeMethod();
       }

       public void initializeMethod ()
       {
            AnotherObject [] anotherObjectArray = new AnotherObject [1];
            for (AnotherObject i: anotherObjectArray)
            {
                i.init(72,72);
            }
       }
   } 

问题出在您的initializeMethod中:

        AnotherObject [] anotherObjectArray = new AnotherObject [1];
        for (AnotherObject i: anotherObjectArray)
        {
            i.init(72,72);
        }

转到Object[] myArray = new Object[1]仅为数组分配空间。 它实际上并没有创建这些对象。 您的代码块应为

        AnotherObject [] anotherObjectArray = new AnotherObject [1];
        anotherObjectArray[0] = new AnotherObject();
        for (AnotherObject i: anotherObjectArray)
        {
            i.init(72,72);
        }

这有点笨拙。 我假设您需要将AnotherObject放入数组中,而您只是不显示它。 如果只需要这些对象之一,则直接创建它。 您可能应该看看列表

我相信这是设计使然。 一个原语将在堆栈上分配,并且默认情况下始终具有某种值。 对象在堆上分配,并将通过引用传递。 在初始化之前,默认情况下,这些引用将不指向任何内容。

您刚刚初始化了AnotherObject对象的数组...但是该数组的第一个字段中没有AnotherObject ...当您获取它时,它为null,因为您没有初始化它。 在initializeMethod中,您必须执行以下操作:

AnotherObject [] anotherObjectArray = new AnotherObject [1];
 for (AnotherObject i: anotherObjectArray)
    {
        i = new AnotherObject();
        i.init(72,72);
    }

干杯!

Java编译器将迫使您初始化变量,原语或引用类型(如对象),否则将生成编译器错误并失败。

但是,数组初始化将创建所需大小的数组,并将每个元素设置为默认值。 对于基元,对于数字类型,这类似于0 ,对于布尔值,则为false 对于引用类型 ,默认值为null 这是您问题的根源。

您正确初始化数组,但不要用有效值填充它。 因此,这与传递引用无关,而与适当的数组初始化有关,并且知道新初始化数组的默认值。

具体来说,您正在使用for-each构造遍历数组。 此构造对集合/数组中的每个对象执行操作。

因此,此代码:

AnotherObject [] anotherObjectArray = new AnotherObject [1];
for (AnotherObject i: anotherObjectArray){
    i.init(72,72);
}

等效于以下代码:

AnotherObject [] anotherObjectArray = new AnotherObject [1];
for(int count=0; count <  anotherObjectArray ; count++){
    AnotherObject tempObj = anotherObjectArray[i]; // This is null, because anotherObjectArray[i] hasn't been initialised yet
    tempObj.init(72,72); // This is where we get the NullPointerException :(
}

你看到问题了吗? 您正在尝试对null执行操作,因此NullPointerException

我相信您的实际意图是:

AnotherObject [] anotherObjectArray = new AnotherObject [1];
for(int count=0; count <  anotherObjectArray ; count++){
    AnotherObject tempObj = new AnotherObject(); // Here we create the new object
    tempObj.init(72,72); // Initialise the newly created object
    anotherObjectArray[i] = tempObj; // Now we store in the array :)
}

我希望我已经足够清楚地解释了这一点,但是请问是否需要澄清:)

暂无
暂无

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

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