Checking Left/Right in an int array for Tetris, Android/Java

977 views Asked by At

I'm trying to make a tetris game for android to help learn game programming for android. My goLeft/Rights break right when the button is pressed, the code for going left is in a class separate of the fields int array, and the list parts array. The fields array is accessed by a referenced variable (TetrisWorld tetrisworld;). While part list array is public so accessed through a variable(part) code for which is in the goLeft() code. It breaks at: if(tetrisworld.fields[x][part.y] != 0) Code for left:

public void goLeft() {
    int x = 0;
    for(int i = 0; i < 4; i++) {
        TetrisParts part = parts.get(i);
        x = part.x - 1;
        if(tetrisworld.fields[x][part.y] != 0) {
            noleft = true;
            break;
        }
    }
    if(noleft == false) {
        for(int i = 0; i < 4; i++) {
            TetrisParts part = parts.get(i);
            part.x--;
        }
    }
}

The code for the fields int array:

int fields[][] = new int[WORLD_WIDTH][WORLD_HEIGHT];

WORLD_WIDTH and WORLD_HEIGHT are both static final ints, width being 9 and height being 19

I've tried putting if(tetrisworld.fields[0][0] == 0) and it still crashes so I don't think it has to do with the variables. Also It doesn't go out of bound even if I haven't added the code to check for that yet because I have the teroid spawning around x = 5 and since I can't go left/right once there's not a chance of that happening

I've tried moving the goLeft/Right methods to the gamescreen class which has a "world = TetrisWorld();" and it still bugs out at the same spot

UPDATE:

Ok just adding:

tetrisworld != null

to the first if statement fixed it, my question now is, why did it fix it? Why can't I move without this check? It clearly isn't null cause as far as I know; it's fully responsive now.

But an easier way to have solved this which is SOOOO easy is changing fields to static... then access it lika so: TetrisWorld.fields so my updated code is:

public void goLeft()
  {
  noleft = false;
  for (int i = 0; i < 4; i++)
     {
     part = parts.get(i);
     if (part.x - 1 < 0 || TetrisWorld.fields[part.x - 1][part.y] != 0)
        {
        noleft = true;
        break;
        }
     }
  if (noleft == false)
     {
     for (int i = 0; i < 4; i++)
        {
        part = parts.get(i);
        part.x--;
        }
     }
  }
3

There are 3 answers

2
Matthew On BEST ANSWER

Ok I believe I found the answer, referencing: http://en.wikipedia.org/wiki/Null_Object_pattern

Apparently java will throw an NPE if you don't check for it first if you have a null reference? Is there any way to initialize it without doing a TetrisWorld tetrisworld = new TetrisWorld(); because it's already created in a different class so i get a thousand errors, an actual stack overflow! lul... Still not 100% positive. Please comment to verify and possibly suggest a better way to go about this.

3
Free Wildebeest On

It looks like you're getting a java.lang.NullPointerException when trying to access the array in tetrisworld. In the line you mention there are several ways that this could occur:

if(tetrisworld.fields[x][part.y] != 0) {
  1. tetrisworld could be null.
  2. The fields member of tetrisworld could be null.
  3. The second array that you're looking up by using tetrisworld.fields[x].
  4. The value of part could be null.

Having a quick look through your source code it looks to me like you never initialise tetrisworld, either at declaration using:

TetrisWorld tetrisworld = new TetrisWorld();

Or at some other point which is certain to have happened before your goLeft() method is called.

3
Olegas On

Looks like you are hitting IndexOutOfBoundsException.

When you are doing x = part.x - 1;, your x variable can become lesser tan zero, thus your code will act like if(tetrisworld.fields[-1][part.y] != 0