user2374242
user2374242

Reputation: 1

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. I tried to fill x and y instance variables of each Point in the array with essentially array[iterator].x=iterator.

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)

Upvotes: 0

Views: 165

Answers (5)

swapnil_nerd
swapnil_nerd

Reputation: 69

You are getting NullPointerException because:

You have created array of point objects and to initialize point object you should create point object first by using new keyword. But here you are not creating memory for point objects that is the reason behind this exception.

Upvotes: 1

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

Just because your array contains null elements.

You have:

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

suppose, area = 2, your array will be:

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;
...

Upvotes: 2

AllTooSir
AllTooSir

Reputation: 49372

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. Refer to the JLS, which tells us that primitive types in Java are always zero-initialized. References are initialized to null.So in an array of references the default value of each of the element will be null reference.

You need to change your lines like below:

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

Upvotes: 2

skuntsel
skuntsel

Reputation: 11742

The following line is absent: map[n] = new Point(); before you make any operations on the array objects, as they are null at that moment.

Upvotes: 0

Dima
Dima

Reputation: 8652

probably because array[iterator]==null or array==null nothing more. why dont you just debug?

Upvotes: 1

Related Questions