This discussion is archived
8 Replies Latest reply: Nov 2, 2012 2:31 AM by Tolls RSS

java.lang.String cannot be cast to java.util.Map

adf009 Explorer
Currently Being Moderated
hi i have the following method,when i click my button am geting this error java.lang.String cannot be cast to java.util.Map

Edited by: adf0994 on 2012/12/19 2:46 PM
  • 1. Re: java.lang.String cannot be cast to java.util.Map
    gimbal2 Guru
    Currently Being Moderated
    adf0994 wrote:
    Map<String, String>username = (Map)actionEvent.getComponent().getAttributes().get("test");
    Well it would be a safe bet that a username is not a Map, but a String.
  • 2. Re: java.lang.String cannot be cast to java.util.Map
    adf009 Explorer
    Currently Being Moderated
    if i put String username = (String)actionEvent.getComponent().getAttributes().get("test");
    i get error assignRoleToUsers(String,String) cannot invoke assignRoleToUsers(String,Map)
  • 3. Re: java.lang.String cannot be cast to java.util.Map
    gimbal2 Guru
    Currently Being Moderated
    Yes but that's just because you don't understand the code you're trying to hack. I can't help you with that, you'll need to make an effort to try and understand it yourself.

    Take a look at the names of the variables and the parameters. You're trying to put a single USERNAME in a parameter which takes a MAP of USERS. Plural, not singular.
  • 4. Re: java.lang.String cannot be cast to java.util.Map
    rp0428 Guru
    Currently Being Moderated
    >
    hi i have the following method,when i click my button am geting this error java.lang.String cannot be cast to java.util.Map
    >
    And that is correct - you can't do that cast. Which is what this code is trying to do.
    >
    (Map)actionEvent.getComponent().getAttributes().get("test")
    >
    Ask yourself what object is supposed to be the 'Map' in that line of code?

    Is it 'actionEvent', 'getComponent()' or 'getAttributes()'?

    My guess is that it is the 'getAttributes' that returns a map. Which means you need to cast
    actionEvent.getComponent().getAttributes()
    as a Map. Which means you need to put that entire thing in parentheses so that the cast is performed on it. And then you have to put THAT entire thing in parentheses so you can call the 'get' method on it.

    So this is the 'Map'
    (Map) (actionEvent.getComponent().getAttributes())
    And this calls the Maps 'get' method
    ( (Map) (actionEvent.getComponent().getAttributes()) ).get("test")
    If you broke the line into pieces and stored each piece in its own variable you would see it. These object types may not be right but should give you the idea.
    //( (Map) (actionEvent.getComponent().getAttributes()).get("test") ).get("test")
    MyComponent myComponent = (MyComponent) actionEvent.getComponent();
    
    Map myMap = (Map) myComponent.getAttributes();
    
    myMap.get("test");
  • 5. Re: java.lang.String cannot be cast to java.util.Map
    Tolls Journeyer
    Currently Being Moderated
    But getAttributes() is already returning a Map.
    So casting is a little pointless.
  • 6. Re: java.lang.String cannot be cast to java.util.Map
    rp0428 Guru
    Currently Being Moderated
    >
    But getAttributes() is already returning a Map.
    >
    Well maybe I missed it but I don't see anything that shows what 'getAttributes' returns.

    But I am pretty sure that OP has the cast on the wrong thing because of the missing parenthesis.
  • 7. Re: java.lang.String cannot be cast to java.util.Map
    gimbal2 Guru
    Currently Being Moderated
    No, the OP is wrongly trying to cast a String to a Map because the method he's trying to invoke wants a Map.
  • 8. Re: java.lang.String cannot be cast to java.util.Map
    Tolls Journeyer
    Currently Being Moderated
    rp0428 wrote:
    >
    But getAttributes() is already returning a Map.
    >
    Well maybe I missed it but I don't see anything that shows what 'getAttributes' returns.

    But I am pretty sure that OP has the cast on the wrong thing because of the missing parenthesis.
    javax.faces.event.ActionEvent.getComponent returns a UIComponent.
    UIComponent.getAttributes returns a Map.
    http://docs.oracle.com/javaee/6/api/javax/faces/event/FacesEvent.html#getComponent()
    http://docs.oracle.com/javaee/6/api/javax/faces/component/UIComponent.html#getAttributes()

    As gimbal2 says, the OP doesn't understand the code they're looking at and is trying to shoehorn the wrong object into a method call.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points