ShoeLace1291 ShoeLace1291 - 1 year ago 50
Java Question

How can I store an array of objects in a class property in java?

I have a class called Tile which represents a tile in a game. In my main screen, I have a loop that draws the tiles on the user's screen. I want to turn each of these tiles into their own objects. I need to store the data for each tile(coordinates, etc) in an array so that I can access them later. I also need to store this array in a class property so I can access them in any method of the main screen class. My code throws an exception on the line

boardTiles[totalTiles] = new Tile(...);
I assume it's because I am improperly assigning the objects to the array somehow. How do I fix this?

The main screen java code:

public class MyGame extends JPanel {

public Tile[] boardTiles;

private void drawHexGridLoop(Graphics g, Point origin, int size, int radius, int padding) {
double ang30 = Math.toRadians(30);
double xOff = Math.cos(ang30) * (radius + padding);
double yOff = Math.sin(ang30) * (radius + padding);
int half = size / 2;

int[] diceSpaces = {2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12};
diceSpaces = shuffleInts(diceSpaces);

String[] resourcesDefault = new String[]{
"Lumber", "Lumber", "Lumber", "Lumber",
"Whool", "Whool", "Whool", "Whool",
"Stone", "Stone", "Stone",
"Wheat", "Wheat", "Wheat", "Wheat",
"Brick", "Brick", "Brick",

String[] resources = shuffleStrs(resourcesDefault);

int totalTiles = 0;
for (int row = 0; row < size; row++) {
int cols;
cols = size - java.lang.Math.abs(row - half);
for (int col = 0; col < cols; col++) {
int xLbl = row < half ? col - row : col - half;
int yLbl = row - half;
int x = (int) (origin.x + xOff * (col * 2 + 1 - cols));
int y = (int) (origin.y + yOff * (row - half) * 3);
String resource;
resource = resources[totalTiles];
int assignedDice;
assignedDice = 0;
} else {
assignedDice = diceSpaces[totalTiles];
drawHex(g, x, y, radius, resource, assignedDice);
boardTiles[totalTiles] = new Tile(totalTiles, xLbl, yLbl, resource, diceSpaces[totalTiles]);

import java.awt.*;
import javax.swing.*;
public class Tile {

public int xCoord;
public int yCoord;
public int xPoly;
public int yPoly;
public String resource;
public int diceNumber;
//Starts in top left, goes clockwise. Indicates what occupies a corner(settlement or city and player that owns it.
public String Corner1;
public String Corner2;
public String Corner3;
public String Corner4;
public String Corner5;
public String Corner6;
//Starts on top, goes clockwise. Indicates if a player's road occupies a certain side of the tile.
public String Side1;
public String Side2;
public String Side3;
public String Side4;
public String Side5;
public String Side6;
public boolean hasRobber;
private PolygonObject hexagon;

Tile(int ID, int posX, int posY, String resource, int diceNumber){

this.diceNumber = diceNumber;
this.resource = resource;


Answer Source

You need to initialize the array of board tiles before you actually use it:

public Tile[] boardTiles = new Tile[size*size];