Kiran
Kiran

Reputation: 199

Type mismatch: cannot convert from Object to List

 CompanyDAO companyDAO = new CompanyDAO();
 List companyList = companyDAO.getAllCompanyList();

MycompanyList contains a data of like this :

 [[1, Agastha Medical Center], [2, Agastha Asthma]]

Now i want to iterate the values and i want to pass a value of 1 to query but when i am placing this inside of a for loop i am getting

for(int k=0;k<=companyList.size();k++){

List companyId =companyList.get(k);  //  [1, Agastha Medical Center]  for k=0;

Type mismatch: cannot convert from Object to List

I need to read value of 1 alone inside of for loop how can i do this ?

Upvotes: 4

Views: 20477

Answers (4)

Engineer
Engineer

Reputation: 1436

you have to type cast the list

for(int k=0;k<=companyList.size();k++){

List companyId =(List)companyList.get(k);

Upvotes: 2

krypton-io
krypton-io

Reputation: 713

Provide the Type of list you want to get from CompanyDAO

Ex. ArrayList<Company>, List<Company>

Upvotes: 1

TheLostMind
TheLostMind

Reputation: 36304

  1. Don't use raw types use generics. example : Arraylist<String>
  2. List companyId =companyList.get(k) is your error. companyList.get(k) returns an object. you have to typecast it to appropriate type explicitly.

Upvotes: 1

Smutje
Smutje

Reputation: 18123

As your companyList is of raw type, you have to cast the object obtained from it explicitly

List companyId = (List) companyList.get(k);

But it would be better to provide your API with types so that casting is not necessary.

Upvotes: 2

Related Questions