简体   繁体   English

为什么此代码会导致NullPointerException?

[英]Why does this code cause a NullPointerException?

I made a code with that iterates through a single dimensional array of objects of type Point from java.awt.Point. 我编写了一个代码,该代码遍历java.awt.Point中Point类型的对象的一维数组。 I tried to fill x and y instance variables of each Point in the array with essentially array[iterator].x=iterator . 我试着用array[iterator].x=iterator填充数组中每个Point的x和y实例变量。

The code 编码

package onmap;

import java.awt.Point;

public class OnMap {

    public static void main(String[] args) {

        int width=50;
        int height=50;
        int area = width * height;
        int xn;
        int yn;
        int i=0;
        int t=0;
        Point[] map;
        map = new Point[area];
        map[i].x=0;
        System.out.print("first x:" + map[i].x);


        for (int n=0; n<area-1;n++){ 
           if (i==width)
           {i=0; t++;}
           map[n].x=i;
           map[n].y=t;
           i++;       
           }

        for (int n=0;n<area-1;n++){
            xn = map[n].x;
            yn = map[n].y;
            System.out.print("x: " + xn);
            System.out.print("  y: "+yn);
            System.out.println("  n: "+n);
             }


    }

}

I don't understand. 我不明白 Why am I receiving a Null Pointer Exception? 为什么我会收到一个空指针异常?
(Netbeans 7.3, Java7) (Netbeans 7.3,Java7)

Because when you initialize 因为当你初始化

    Point[] map;
    map = new Point[area]; 

It contains all null references.It creates an array of Point with each element in the array by default initialized as Point element=null .So, when you try map[0].x it will obviously throw NullPointerException as map[0]==null . 它包含所有空引用。它创建一个Point数组,默认情况下将数组中的每个元素初始化为Point element=null 。因此,当您尝试map[0].x ,它将显然抛出NullPointerException作为map[0]==null Refer to the JLS , which tells us that primitive types in Java are always zero-initialized. 参考JLS ,它告诉我们Java中的原始类型始终为零初始化。 References are initialized to null.So in an array of references the default value of each of the element will be null reference. 引用被初始化为null,因此在引用数组中,每个元素的默认值将为null引用。

You need to change your lines like below: 您需要像下面这样更改行:

Point[] map;
map = new Point[area];
map[i] = new Point();
map[i].x=0;

Just because your array contains null elements. 仅因为您的数组包含空元素。

You have: 你有:

...
map = new Point[area];
map[i].x=0;
...

suppose, area = 2 , your array will be: 假设area = 2 ,您的数组将是:

map[0] = null;
map[1] = null;

You could correct it by doing the following change: 您可以通过以下更改来更正它:

...
map = new Point[area];
// initialize Point array
for (int k=0; k < area; k++) {
    map[k] = new Point();
}
// ends initialization
map[i].x = 0;
...

probably because array[iterator]==null or array==null nothing more. 可能是因为array[iterator]==nullarray==null why dont you just debug? 你为什么不调试呢?

You are getting NullPointerException because: 您收到NullPointerException的原因是:

You have created array of point objects and to initialize point object you should create point object first by using new keyword. 您已经创建了点对象数组,并且要初始化点对象,您应该首先使用new关键字创建点对象。 But here you are not creating memory for point objects that is the reason behind this exception. 但是在这里,您没有为点对象创建内存,这是导致此异常的原因。

The following line is absent: map[n] = new Point(); 缺少以下行: map[n] = new Point(); before you make any operations on the array objects, as they are null at that moment. 在对数组对象进行任何操作之前,因为此时它们为null。

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

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