Results 1 to 9 of 9
  1. #1
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default How to fix this "incompatible types" issue

    In the code below if I don't use a cast:

    Java Code:
    ArrayList<Instance> currentEvent = mapItem.getValue();
    I get:

    Java Code:
     incompatible types
    found   : java.lang.Object
    required: java.util.ArrayList<Instance>
    				ArrayList<Instance> currentEvent = mapItem.getValue();

    If I use a cast:

    Java Code:
    ArrayList<Instance> currentEvent = (ArrayList<Instance>) mapItem.getValue();
    I get

    Java Code:
    warning: [unchecked] unchecked cast
    found   : java.lang.Object
    required: java.util.ArrayList<Instance>
    				ArrayList<Instance> currentEvent = (ArrayList<Instance>) mapItem.getValue();
    mapItem is aMap.Entry. What am I doing wrong ?
    thanks

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you may add items to an arraylist like this:

    currentEvent.add(Object)

    but the object should match the ArrayList type in this case: Instance


    but the point is currentEvent = Object is wrong syntax.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default

    @ozzyman But i don't need to add an item to an ArrayList, I'm getting an ArrayList and that's it.

    @Fubarable These are the initializations

    Java Code:
    instancesLists = new HashMap<String, ArrayList<Instance>>();
    ...
    Iterator it = instancesLists.entrySet().iterator();
    	while (it.hasNext()) {
    		Map.Entry mapItem = (Map.Entry)it.next();
    thanks

  5. #5
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default

    Any help ? I'm a bit stuck

    I'm currentEvent is a List i'm retrieving from the mapItem, I'm not adding items to currentEvent.

    mapItem contains Lists

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aneuryzma View Post
    Any help ? I'm a bit stuck

    I'm currentEvent is a List i'm retrieving from the mapItem, I'm not adding items to currentEvent.

    mapItem contains Lists
    Your Iterator is a 'raw' iterator; it should be an:

    Iterator<Map.Entry<String, ArrayList<Instance>>>

    object instead. Raw iterators just return Objects; you don't want that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

    Default

    I've changed as you suggest, the iterator declaration

    Java Code:
    Iterator<Map.Entry<String, ArrayList<Instance>>> it = instancesLists.entrySet().iterator();
    but I still get the same error

    Java Code:
    incompatible types
    found   : java.lang.Object
    required: java.util.ArrayList<Instance>
    				ArrayList<Instance> currentEvent = mapItem.getValue();
    This is my code: #1722600 - Pastie

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aneuryzma View Post
    I've changed as you suggest, the iterator declaration

    Java Code:
    Iterator<Map.Entry<String, ArrayList<Instance>>> it = instancesLists.entrySet().iterator();
    but I still get the same error

    Java Code:
    incompatible types
    found   : java.lang.Object
    required: java.util.ArrayList<Instance>
    				ArrayList<Instance> currentEvent = mapItem.getValue();
    This is my code: #1722600 - Pastie
    Then you're using a raw Map.Entry type which implies that you're using a raw Map somewhere.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    aneuryzma is offline Member
    Join Date
    Aug 2008
    Posts
    46
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. incompatible types...What is "Dispatch" ?
    By ollyworks in forum Java Applets
    Replies: 1
    Last Post: 09-10-2009, 03:03 AM
  3. Facing issue in printing user like "ab\\cd"
    By psmhelp in forum New To Java
    Replies: 4
    Last Post: 04-27-2009, 01:28 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •