0

I am getting an error on this line:-"for(Item item:Items)" on "Items" keyword and the error is "Type mismatch: cannot convert from element type Object to Item". somebody Please help me out for this.

import java.util.ArrayList;

public class Model 
{
    public static ArrayList Items;

    public static void LoadModel()
    {
        Items=new ArrayList();
        Items.add(new Item(1, "taj.png", "Taj Mahal", "arrow.png"));
        Items.add(new Item(2, "agra_fort.png", "Agra Fort", "arrow.png"));
        Items.add(new Item(3, "chini _ka_rauza.png", "Chini ka Rauza", "arrow.png"));
        Items.add(new Item(4, "fatepur_sikri.png", "Fatepur Sikri", "arrow.png"));
        Items.add(new Item(5, "guru_ka_tal.png", "Guru Ka Tal", "arrow.png"));
        Items.add(new Item(6, "jamma_masjid.png", "Jamma Masjid", "arrow.png"));
        Items.add(new Item(7, "mankameshwar_temple.png", "Mankameshwar", "arrow.png"));
        Items.add(new Item(8, "mehtab_baug.png", "Mehtab Baug", "arrow.png"));
        Items.add(new Item(9, "musamman_burj.png", "Musamman Burj", "arrow.png"));
        Items.add(new Item(10, "panch_mahal.png", "Panch Mahal", "arrow.png"));


    }
    public static Item GetbyId(int id)
    {
        for(Item item:Items)
        {
            if(item.id==id)
            {
                return item;
            }
        }
        return null;
    }

}
14
  • Try to declare list this way : public static ArrayList<Item> Items; Commented Jul 2, 2014 at 10:10
  • Hey thnx..the error is solved but my project isnt working..i am getting force close. Commented Jul 2, 2014 at 10:23
  • Try to print both item.id and id value before compare it. Commented Jul 2, 2014 at 10:27
  • Why are you create custom holder class from item you can simply hold this type data using HashMap if you prefer. Commented Jul 2, 2014 at 10:31
  • Sorry but i dint get you..can you explain little more. Commented Jul 2, 2014 at 10:33

2 Answers 2

2

First initialize the ArrayList as

  ArrayList<Item> Items = new ArrayList<Item>();
Sign up to request clarification or add additional context in comments.

Comments

0

It should be Items= new ArrayList<Item>(); Otherwise Java assumes the worst case of Object.

2 Comments

Hey thnx..but its not working..can you suggest me something more..?
Oh, as @Aniruddha mentioned- you need the <Item> in the declaration as well.

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.