1

I'm trying to get a string from the content of an object with a custom method in my Android application. The object is an instance of the class Game and is contained in an arraylist:

public class Game {

    private static List<Game> games = new ArrayList<Game>();
    private String teamName1;
    private String teamName2;
    private int goalsP1;
    private int goalsP2;

    private String result;


    public Game(String teamName1, String teamName2, int goalsP1, int goalsP2)
    {
        this.teamName1 = teamName1;
        this.teamName2 = teamName2;
        this.goalsP1 = goalsP1;
        this.goalsP2 = goalsP2;
    }

    public static List getGames() {
    return games;
    }

    public static void addGameToArray(Game randGame) { games.add(randGame);}

    public static String getStringFromGame(Game randGame) {
        randGame.result = randGame.result + randGame.teamName1 + randGame.goalsP1 + randGame.goalsP2 + randGame.teamName2;
        return randGame.result;
    }

In my MainActivity I'm trying to call the getStringFromGame:

    Game game1 = new Game("Man City ","Real Madrid", 5 ,0);

    Game.addGameToArray(game1);

    String result = Game.getStringFromGame(Game.getGames().get(0));

I get an error:

getStringFromGame cannot be applied to java.lang.Object

What is going wrong?

Aelian
  • 23
  • 6
  • 3
    Where is `getGames` defined? – resueman Mar 10 '16 at 19:12
  • Seems like the `getGames` method is missing or returning a wrong type. Make sure you parameter of the method is of type Game. That is why it returns an error. Because You are trying to set a different type in. Like here in this [example](http://stackoverflow.com/questions/5410758/java-lang-string-cannot-be-applied-to-java-lang-object) – Jernej K Mar 10 '16 at 19:18

3 Answers3

2

It looks like you're returning a generic list with getGames().

So define it as

public static List<Game> getGames() { return games; }

and everything should work fine.

tynn
  • 38,113
  • 8
  • 108
  • 143
0

Java is statically typed. This error message translates to "You have passed an Object to getStringFromGame, which we know takes a Game.

You don't show your getGames definition, but the error message indicates you're returning an Object value from it. Because Game extends Object (and not the other way around), you have a problem. A function that is expecting a Game will reject being passed an Object.

Given the get(0) here:

String result = Game.getStringFromGame(Game.getGames().get(0));

The return type of getGames() is probably a Collection of some sort. If the type of a collection is not specified, Java assumes everything in that collection is of type Object. Java Generics can be used to define what it is a collection of. So rather than a signature like this:

 public List getGames() { ... }

You want something like this:

 public List<Game> getGames() { ... }

(Note that List is a subtype of Collection. If you care about order, then use List, otherwise you should replace it with Collection).

Further, it seems to indicate that you are randomizing the order of that list. In which case an even better solution would be:

 public Game getRandomGame() { ... }

Which avoids the issue of generics altogether and keeps the randomization encapsulated.

Post-Update Addendum

As a minor aside, this might get you into trouble:

public class Game {

    private static List<Game> games = new ArrayList<Game>();
    ...
    public static List getGames() {
        return games;
    }
    ...
}

Because you're declaring games as a static member field, any instantiated game may update that. This can get confusing and hard to predict very fast. It might be better to put these in a separate class (GameLibrary?) like so:

public class GameLibrary {
     private List<Game> games = new ArrayList<Game>();

     public List getGames() {
         return games;
     }
}

This way, code using one instance of GameLibrary has no access to every other instance of GameLibrary. As it stands, any instantiation of Game can modify your the private static List<Game> games value... which breaks encapsulation.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • 1
    Thanks for your answer and great explanation! My code works fine now. I added the getGames method in the code of my question. – Aelian Mar 10 '16 at 20:06
  • @Aelian Glad to help! Definitely check out my addendum: may help guard against future problems. – Nathaniel Ford Mar 10 '16 at 20:26
0

Your getGames() method obviously returns a List<Something?!> instead of a List<Game>. You can't pass an argument that is not a Game or it's subclass to the getGames() method since it expects a Game to be passed.

Just make the method return a List<Game> and it'll work.

Vucko
  • 7,371
  • 2
  • 27
  • 45