1

I have a suspicion that the makeLine method created a null array somehow, but I'm not quite sure.

Any help would be greatly appreciated! A snippet of my code is below:

public void all() {

        int[][] line = new int[3][];

        for (int i = 0; i < 3; i++) {
            if (i > 0 && line[i - 1][0] == 2) { // Error occurs at this line
                int start = line[i - 1][2];
                int pos = line[i - 1][1];
                int xy = line[i - 1][3]++;

                if (line[i - 1][3] == 1) {
                    int end = y()[1];
                    line[i] = new int[]{1, start, end, pos, xy};
                } else {
                    int end = x()[1];
                    line[i] = new int[]{1, start, end, pos, xy};
                }
            } else {
                line[i] = makeLine();
            }
        }
}

private int[] makeLine() {
                Random r = new Random();

                int startX = x()[0];
                int endX = x()[1];
                int startY = y()[0];
                int endY = y()[1];

                int xy = r.nextInt(3 - 1) + 1;

                if (xy == 1) {
                    return new int[]{1, startX, endX, startY, xy};
                }

                return new int[]{1, startY, endY, startX, xy};
            }

private int[] x() {
        DisplayMetrics displaymetrics = context.getResources().getDisplayMetrics();
        int xLeft = (int) (13 * displaymetrics.density);
        int xRight = (int) (displaymetrics.widthPixels - (13 * displaymetrics.density));

        Random r = new Random();
        int startX = r.nextInt(xRight - xLeft) + xLeft;
        int endX = r.nextInt(xRight - xLeft) + xLeft;

        return new int[]{startX, endX};
    }

private int[] y() {
        DisplayMetrics displaymetrics = context.getResources().getDisplayMetrics();
        int yTop = (int) (60 * displaymetrics.density);
        int yBottom = (int) (displaymetrics.heightPixels - (51 * displaymetrics.density));

        Random r = new Random();
        int startY = r.nextInt(yBottom - yTop) + yTop;
        int endY = r.nextInt(yBottom - yTop) + yTop;

        return new int[]{startY, endY};
    }
2
  • 1
    Can you post the full error/stacktrace that you're getting? Commented Jan 2, 2017 at 0:45
  • I don't think there's enough information here to help you. You need to provide us with a complete example we can run. At the moment this is impossible because some methods (e.g. x() and y()) are missing. Commented Jan 2, 2017 at 0:54

2 Answers 2

1

The exception is because you tried accessing line[0][0] before it was initialized, there is nothing there.

By doing:

int[][] line = new int[3][];

You are only creating 1 dimension for this multidimensional array. The way to do this is either:

int[][] line = new int[3][SOME_NUMBER];

or to explicitly initialize each row:

int[][] line = new int[3][];
for (int i = 0; i < 3; i++) {
  line[i] = new int[SOME_NUMBER];
}

If you need to better understand it, have a look here, for example: [Multi-Dimensional Arrays in Java

]1

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are getting a null pointer because you made a reference to an element outside the bounds of the array. The line where you got the error on should look like this:

if (i > 0 && line[i][0] == 2) {

Instead of:

if (i > 0 && line[i - 1][0] == 2)

There are mistakes similar to this all over this program. Remember that arrays start indexing at 0, not 1.

1 Comment

There is nothing wrong with accessing line[i - 1][0], or accessing line[i][0], if the memory has been allocated.

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.