Reputation: 1047
I'm doing a query search on a database, that returns a list. The List is filled with variables type Object. I do an query.get(0).getClass() and it returns (something).WorkbookMappingClass. But the thing is I have to Cast that object to an WorkbookMapping object. But its giving me an I/O error(maybe its because of the Exception being practically global.)
Here's the code:
WorkbookMapping wk;
List query = ses.createQuery("FROM Workbook").list();
System.out.println("Class: "+query.get(query.size()).getClass());//return ...WorkbookMapping
wk =(WorkbookMapping)query.get(query.size());
Anybody have an idea?
Upvotes: 1
Views: 78
Reputation: 4239
WorkbookMapping wk;
List query = ses.createQuery("FROM Workbook").list();
System.out.println("Class: "+query.get(query.size()).getClass());//return ...WorkbookMapping
Object obj = query.get(query.size() - 1)
if(obj instanceof WorkbookMapping) {
wk =(WorkbookMapping);
}
Irrespective of that, as Charlesworth
suggested, you should have either a ClassCastException
or ArrayIndexOfBoudsException
(as you are accessing with query.size()
)
Upvotes: 1
Reputation: 5818
The last element of the List will be the query.size()-1 as long as the Lists are 0-based like arrays. So query.get(query.size()) returns null in your example.
Upvotes: 1