I have an method to get list of contacts from database. It return a List
public List<String[]> getContacts(String param) {
...
Query q = this.getCurrentSession().createSQLQuery("
select first_name, last_Name, email_address, fax
from contacts
where first_name = :param");
q.setParameter("param", param);
List<String[]> listResult = q.list();
return listResult;
}
When I get Contact name using following code:
List<String[]> contacts = (ArrayList<String[]>) contactManager.getContacts(fistName);
if (contacts != null && contacts.size() > 0) {
person.setLastName(contacts.get(0)[1]); //error line
}
I get the error:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
I change source code to:
List<String[]> contacts = (ArrayList<String[]>) contactManager.getContacts(fistName);
if (contacts != null && contacts.size() > 0) {
Object[] objTemp = contacts.get(0);
String temp = (String) objTemp[1];
person.setLastName(temp);
}
It runs well!
I have debugged and the object returned from getContacts() function is still the List. Look like object just cast to List at that time the error line run.
Please help me to explain why the second code can run without error. I think 2 sources are same logic.