-2

i get this error, Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.cave.Area.setTiles(android.widget.ImageView)' on a null object reference

i tryed to make anything i know...

//this the main
Area[] area = new Area[9];
    ImageView[] tiles = new ImageView[9];
    for(int i=1; i<=9; i++)
    {
        area[i].setTiles(tiles[i]);
    }
//this is the second class
public class Area  {

ImageView tiles ;

public ImageView getTiles() {
    return tiles;
}

public void setTiles(ImageView tiles) {
    this.tiles = tiles;
}



//
  i want to create tiles, 9 x 9, this is for area 1, than i want another area, 
  area 2, area 3 ... area 9... i want to create imageview array to call it in a 
  class of areas array...
//
dodo
  • 38
  • 12

1 Answers1

0

You have created an array of Area objects, but haven't created the objects themselves.

//this the main
Area[] area = new Area[9];
ImageView[] tiles = new ImageView[9];

// iterator from 0 to 8, not from 1 to 9
for(int i=0; i<9; i++)
{
    // create array elements, perhaps with constructors, perhaps in some other way
    area[i] = new Area();
    tiles[i] = new Tile();

    area[i].setTiles(tiles[i]);
}
iluxa
  • 6,941
  • 18
  • 36
  • hi,thx for answering, // iterator from 0 to 8, not from 1 to 9 , whats the difference? – dodo Sep 10 '19 at 21:57
  • Indices in arrays in Java (and most other languages) start at 0. So, a 9-element array will have indices of 0, 1, ..., 8. If you try referencing `array[9]`, you'll get a `NoSuchElementException`. Try this read: https://www.geeksforgeeks.org/arrays-in-java/ – iluxa Sep 10 '19 at 21:58
  • area[i].setTiles(tiles[i]); it doesnt work... how come i got this array Tiles[] tiles = new Tiles[9]; from this class ImageView[] tiles; public ImageView[] getTiles() { return tiles; } public void setTiles(ImageView[] tiles) { this.tiles = tiles; } but when i call it on main class Tiles[] tiles = new Tiles[9]; for (int i = 0; i<=8; i++) { tiles[i] = new Tiles(); tiles[i].setTiles(tiles[i]); } i cant because of area[i].setTiles(tiles[i]); – dodo Sep 11 '19 at 07:34