Java : creating an array of array of objects

Viewed 93

i'm new in Java and working on a small project and having an issue, I hope you can help :

I'm trying to create a 2 dimensional array, inwhich each element is an Object of type Field that holds x and y ( as the coordinate of the element )

I'm throwing an error when the sent parameters for length and width are < 0

I'm testing my code in the main method but the error is always thrown meaning that the method to create the "map" is not receiving the correct parameters.

Note : the main method is in a different class ( main class )

```

import bbb.MyException;

public class CoordinateSystem {

private int length;
private int width;
private Field[][] map = createMap(getWidth(), getLength());

public CoordinateSystem(int width, int length) throws MyException {
    this.width = width;
    this.length = length;
}

public int getLength() {
    return this.length;
}

public int getWidth() {
    return this.width;
}

public class Field{
    private int x;
    private int y;

    public void setX(int x) {
        this.x = x;
    }

    public int getX() {
        return x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getY() {
        return y;
    }

}

public Field[][] getMap() {
    return map;
}

// Initializing a coordinate to each "field"
public Field[][] createMap(int width, int length) throws MyException {
    if(width > 0 && length > 0){
        Field[][] map = new Field[width][length];
        for( int i = 0 ; i < width ; i++ ){
            for( int j = 0 ; j < length  ; j++ ){
                map[i][j].setX(j);
                map[i][j].setY(i);
            }
        }
        return map;
    } else{
        throw new MyException("Sorry, can't create a field of width or height = 0 ");
    }
}

}

 public static void main(String[] args) throws MyException {

    CoordinateSystem board = new CoordinateSystem(8, 9);

    for( int i = 0 ; i < 8 ; i++ ){
        for( int j = 0 ; j < 9  ; j++ ){
            System.out.print(board.getMap()[i][j].getX());
            System.out.println(board.getMap()[i][j].getY());
        }
    }
    Exception in thread "main" bbb.MyException: Error! Sorry, can't create a 
    field of width or height = 0 
    at CoordinateSystem.createMap(CoordinateSystem.java:62)
    at CoordinateSystem.<init>(CoordinateSystem.java:9)
    at Main.main(Main.java:21)

    Process finished with exit code 1
2 Answers

This line of your code (in method createMap())...

Field[][] map = new Field[width][length];

creates a two dimensional array but every element in the array is null.
Hence this line of your code (also in method createMap())

map[i][j].setX(j);

will throw a NullPointerException.

You need to explicitly create Field objects.
Also the Y coordinate of some of the Field elements in the map is zero as well as the X coordinate in some of the elements because (also in method createMap()) you start the for loops with zero. In order to fix that I add one to i and j when I call setX() and setY().

Here is the corrected code for the for loops in method createMap()

for( int i = 0 ; i < width ; i++ ){
    for( int j = 0 ; j < length  ; j++ ){
        map[i][j] = new Field();
        map[i][j].setX(j + 1);
        map[i][j].setY(i + 1);
    }
}

The only thing left to do is call method createMap(). Since map is a member of class CoordinateSystem, it seems logical to call createMap() from the constructor of CoordinateSystem.

public CoordinateSystem(int width, int length) throws MyException {
    this.width = width;
    this.length = length;
    map = createMap(width, length);
}

Finally, for the sake of completeness, here is the entire [corrected] code of class CoordinateSystem

public class CoordinateSystem {

    private int length;
    private int width;
    private Field[][] map;

    public CoordinateSystem(int width, int length) throws MyException {
        this.width = width;
        this.length = length;
        map = createMap(width, length);
    }

    public int getLength() {
        return this.length;
    }

    public int getWidth() {
        return this.width;
    }

    public class Field {
        private int x;
        private int y;

        public void setX(int x) {
            this.x = x;
        }

        public int getX() {
            return x;
        }

        public void setY(int y) {
            this.y = y;
        }

        public int getY() {
            return y;
        }
    }

    public Field[][] getMap() {
        return map;
    }

    // Initializing a coordinate to each "field"
    public Field[][] createMap(int width, int length) throws MyException {
        if(width > 0 && length > 0){
            Field[][] map = new Field[width][length];
            for( int i = 0 ; i < width ; i++ ){
                for( int j = 0 ; j < length  ; j++ ){
                    map[i][j] = new Field();
                    map[i][j].setX(j + 1);
                    map[i][j].setY(i + 1);
                }
            }
            return map;
        }
        else{
            throw new Exception("Sorry, can't create a field of width or height = 0 ");
        }
    }

    public static void main(String[] args) throws MyException {
        CoordinateSystem board = new CoordinateSystem(8, 9);
        for( int i = 0 ; i < 8 ; i++ ) {
            for( int j = 0 ; j < 9  ; j++ ) {
                System.out.print(board.getMap()[i][j].getX());
                System.out.println(board.getMap()[i][j].getY());
            }
        }
    }
}

You're initializing map before initializing width and height, so getWidth() and getHeight() both return 0. You can move the initialization inside the constructor and use the width and height there:

public class CoordinateSystem {

    private int length;
    private int width;
    private Field[][] map;

    public CoordinateSystem(int width, int length) throws MyException {
        this.width = width;
        this.length = length;
        map = createMap(width, height);
    }

    // rest of the class...
Related