2

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
  • 2
    Hi, try initializing map = createMap(getWidth(), getLength()); inside the constructor after width and length have been initialized. Commented Aug 29, 2020 at 0:31
  • @IronMan i'm now getting another error : Exception in thread "main" java.lang.NullPointerException at CoordinateSystem.createMap(CoordinateSystem.java:58) at CoordinateSystem.<init>(CoordinateSystem.java:22) at Main.main(Main.java:21) Commented Aug 29, 2020 at 0:39

2 Answers 2

2

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());
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is working perfectly, thank you so much @Abra. The only thing i needed to change from your code was (i+1) and (j+1) to i and j, because my coordinate system starts with 0,0. Can you please confirm that it won't cause any problem ? Thank you again !
@WassimJaoui it won't cause a problem.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.