0

I have an ArrayList of Tiles with two values; an int and a String.

I need to get both values, so while I was writing the code I decided to make sure it was working:

private void setIcons()
    {
        ArrayList<Tiles> tiles = new ArrayList<>();
        tiles = game.getTiles(0);
        Tiles i = tiles.get(0);
        String s = i.returnTileName();
        System.out.println(i.returnTileName() +" ," +i.returnTileNumber());
    }

This is my Tiles class which has subclasses:

public class Tiles
{
    int number;
    String name;

    public Tiles()
    {

    }

    public String returnTileName()
    {
        return name;
    }

    public int returnTileNumber()
    {
        return number;
    }
}

'Tiles' has several subclasses, all structured almost the same:

public class Bamboo extends Tiles
{
    int number;
    String name;
    public Bamboo(int number, String name)
    {
        this.number = number;
        this.name = name;
    }
}

When I run it, the String is returned as 'null' and my int is returned as '0', when they clearly have values, as the image below will show.

http://img716.imageshack.us/img716/6154/emx4.jpg

The method I call in setIcons() :

 public ArrayList getTiles(int playerX)
    {
        ArrayList<Tiles> tiles = new ArrayList<>();
        Player pl = player.get(playerX);
        tiles = pl.playerTiles;
        return tiles;
    }

The 'Game' class has two ArrayLists in it, 'tilesDeck', an ArrayList of Tiles and 'player', an ArrayList of a class Player.

Here's my game class. Didn't know it had a scroll feature before now.

public class Game
{
    ArrayList<Tiles> tilesDeck = new ArrayList<>();
    ArrayList<Player> player = new ArrayList<>();

    public Game()
    {
        addTile("Bamboo", 1);
        addTile("Bamboo", 2);
        addTile("Bamboo", 3);
        addTile("Bamboo", 4);
        addTile("Bamboo", 5);
        addTile("Bamboo", 6);
        addTile("Bamboo", 7);
        addTile("Bamboo", 8);
        addTile("Bamboo", 9);

        addTile("Circles", 1);
        addTile("Circles", 2);
        addTile("Circles", 3);
        addTile("Circles", 4);
        addTile("Circles", 5);
        addTile("Circles", 6);
        addTile("Circles", 7);
        addTile("Circles", 8);
        addTile("Circles", 9);

        addTile("Characters", 1);
        addTile("Characters", 2);
        addTile("Characters", 3);
        addTile("Characters", 4);
        addTile("Characters", 5);
        addTile("Characters", 6);
        addTile("Characters", 7);
        addTile("Characters", 8);
        addTile("Characters", 9);

        addTile("Dragons", 1);
        addTile("Winds", 1);

        Collections.shuffle(tilesDeck);
        player.add(new Player("Player 1"));
        player.add(new Player("Player 2"));
        player.add(new Player("Player 3"));
        player.add(new Player("Player 4"));

        startTiles();
    }

    private void addTile(String name, int number)
    {
        int counter = 0;
        if (name.contains("Bamboo"))
        {
            while (counter < 4)
            {
                tilesDeck.add(new Bamboo(number, name));
                counter++;
            }
        }

        if (name.contains("Circles"))
        {
             while (counter < 4)
            {
                tilesDeck.add(new Circles(number, name));
                counter++;
            }
        }

        if (name.contains("Characters"))
        {
             while (counter < 4)
            {
                tilesDeck.add(new Characters(number, name));
                counter++;
            }
        }

        if (name.contains("Winds"))
        {
             while (counter < 4)
            {
                tilesDeck.add(new Winds("East"));
                tilesDeck.add(new Winds("South"));
                tilesDeck.add(new Winds("West"));
                tilesDeck.add(new Winds("North"));
                counter++;
            }
        }

        if (name.contains("Dragons"))
        {
             while (counter < 4)
            {
                tilesDeck.add(new Dragons("Red"));
                tilesDeck.add(new Dragons("Green"));
                tilesDeck.add(new Dragons("White"));
                counter++;
            }
        }
    }

    private void startTiles()
    {
        int playerCounter = 0;

        while (playerCounter < 4)
        {
            Player pl = player.get(playerCounter);
            int counter = 0;
            while (counter <13)
            {
                pl.playerTiles.add(tilesDeck.get(0));
                tilesDeck.remove(0);
                counter++;
            }
            playerCounter++;
        }
    }

    public ArrayList getTiles(int playerX)
    {
        ArrayList<Tiles> tiles = new ArrayList<>();
        Player pl = player.get(playerX);
        tiles = pl.playerTiles;
        return tiles;
    }
}
6
  • They clearly don't have values, as you see when you run the program. Commented Oct 17, 2013 at 8:18
  • Please add more information about game and the game.getTiles(0) instruction Commented Oct 17, 2013 at 8:22
  • I don't think there's enough code here for us to spot the bug. Commented Oct 17, 2013 at 8:23
  • The screenshot indicates that i is an instance of a subclass of Tiles. If so, is the method overridden in it? It might be helpful to see its source. Commented Oct 17, 2013 at 8:28
  • Added some code from one of the subclasses. Commented Oct 17, 2013 at 8:31

4 Answers 4

1

The problem is that your subclasses are creating duplicate number and name variables:

public class Bamboo extends Tiles
{
    int number; // <-- remove this
    String name; // <-- and this
    public Bamboo(int number, String name)
    {
        this.number = number;
        this.name = name;
    }
}

Class Bamboo is initializing the duplicate number and name variables, but the returnTileName() and returnTileNumber() methods are returning the uninitialized variables from the Tiles superclass.

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

1 Comment

Thank you! This solved it. If I had enough 'reputation', I'd upvote your reply. Again, thank you very much.
0
public Tiles()
{

}

initialize your variables inside there.

Comments

0

You must initialise your variables in your constructor

1 Comment

I have them initialized in my subclasses... is that good enough? thanks for replying.
0

Initialize your variables with some default value to avoid getting null values.

public class Tiles
{
// assign your default values
int number = 1;           
String name = "default name";

public Tiles()
{

}

public String returnTileName()
{
    return name;
}

public int returnTileNumber()
{
    return number;
}
}

3 Comments

When I change my code to this, it returns '1' and "default name".
yes thats because you have initialized the variables with these values. Change it to whatever you want them to be.
They should get changed in their subclasses? or have I misunderstood something?

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.