1

I'm new to Java and getting an NullPointerException error with this code at this line:

spielfeld[i][j] = new DominionTile(world,i,j); // last function

Here is the whole program code:

public class MapProvider implements ....... {
private DominionTile[][] spielfeld;

int row;
int col;
public MapProvider(int zahl1, int zahl2) {
    DominionTile[][] spielfeld = new DominionTile[zahl1][zahl2];
    col = zahl1;
    row = zahl2;
}

@Override
public MapTile[] getColumn(int spalte) { // DONE
    if ((spalte < 0) && (spalte > col) ) {
        return null;
    }
    else {
        return spielfeld[spalte]; 
    }
}

@Override
public int getColumns() { // DONE
    return col;
}

@Override
public int getRows() { // DONE
    return row;
}

@Override
public boolean isValid(int spalte, int zeile) { // DONE
    if ((spalte < 0) && (zeile < 0)) {
        return false;
    }
    else if ((spalte > col) && (zeile > row)) {
        return false;
    }
    else {
        return true; 
    }
}

@Override
public DominionTile getTile(int col, int row) { // DONE
    return spielfeld[col][row]; 
}

@Override
public void setupMapTiles(MapWorld world) { // NICHT FERTIG
    final Map karte = world.getMap();
    int zeilen = karte.getRows();
    int spalten = karte.getColumns();
    for (int i = 1; i <= spalten; i++) { // I-TE SPALTE
        for (int j = 1; j <= zeilen; j++) { // J-TE ZEILE
            spielfeld[i][j] = new DominionTile(world,i,j);
            //DominionTile neu = new DominionTile(world, i, j);
            //spielfeld[i][j] = (DominionTile)neu;
        }
    }
}

}

The last function should put a DominionTile in each place of the array. What am I doing wrong?

5
  • Can you copy the stacktrace pls? Commented Nov 10, 2014 at 9:39
  • Can you append the Exception's stack trace? Commented Nov 10, 2014 at 9:39
  • Will see : stackoverflow.com/questions/15747516/… Commented Nov 10, 2014 at 9:40
  • Where is the DominionTile(world,i,j) constructor defined. can you also post the code for that? Commented Nov 10, 2014 at 9:41
  • Your arrays are 0-indexed. Don't start from 1 and go all the way up to <=, instead try 0 and <. Commented Nov 10, 2014 at 9:42

2 Answers 2

4

You have this in your constructor. This declares and assigns to a local variable, not the spielfeld field, and hence the field is left with a null value.

DominionTile[][] spielfeld = new DominionTile[zahl1][zahl2];

You probably want:

public MapProvider(int zahl1, int zahl2) {
    spielfeld = new DominionTile[zahl1][zahl2];
    col = zahl1;
    row = zahl2;
}

i.e. without the type declaration, which will assign to the object's field.

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

Comments

0

As a starting point, you might want print out the values of zeilen and spalten. I am guessing this is caused by accessing spielfeld[i][j] where spielfeld[i] does not exist in the first place.

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.