0

The following is my hashmap.

private HashMap<String, Integer> details;
details = new HashMap<>();
details.put("coins", 0);
details.put("score", 0);
details.put("wins", 0);

The following is my get coins method

public int getCoinCount() {
    if(details == null)
        return 0;

    return  details.get("coins").intValue();
}

Now when I run this I get a ClassCastException. The following is the code that is calling it.

int coins =  MyClass.getInstance().getCoinCount();

It says I can't cast a long to an integer! THERE WAS NEVER A MENTION TO A LONG TYPE! WHAT IS GOING ON?

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at game.drew.com.respy.User.User.getCoinCount(RespyUser.java:78)
at game.drew.com.game.Gameplay.ProfileFragment.onCreateView(ProfileFragment.java:226)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

-2

I was hoping your stacktrace would give me more to go off of, unfortunately I'm about as puzzled as you are. This isn't a true fix to your problem, but since you are putting the values into the HashMap as as Integer it might be best to change getCoinCount to also return an Integer and set coins to be an Integer as well.

In the past when I get a primitive value from an Integer I usually end up retrieving it as a long too.

CodyEngel
  • 1,501
  • 14
  • 22