Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Even i'm casting Object into int, But this exception occur...

Actually my Hibernate-JPA method was return Object then i'm converting that Object into int...

Hear is my Hiberanate code..

@Transactional
public Object getAttendanceList(User user){

    Query query = entityManager.createQuery("select Count(ad) from AttendanceDemo ad inner join ad.attendee at  where at.user=:user",
            Long.class);
    query.setParameter("user", user);
    return query.getSingleResult();
}

Now i'm converting this Object as int..

int k = (Integer) userService.getAttendanceList(currentUser);

i'm converting Object to Integer... hear any problem

Please help me...

share|improve this question
    
Looks like getAttendanceList is returning you Long. Can you post the code for the same –  zerocool Jun 18 '13 at 8:29
    
Are you sure you're returning an Integer? ClassCastException says that you're not returning an Integer... –  Matteo N. Jun 18 '13 at 8:31
    
Yes.. i want to store getAttendanceList() method return value into k. then only ClassCastException coming and now i'm casting to (Integer) but again that exception come.. –  SWEE Jun 18 '13 at 8:34

2 Answers 2

up vote 10 down vote accepted

Use:

((Long) userService.getAttendanceList(currentUser)).intValue();

instead.

The .intValue() method is defined in class Number, which Long extends.

share|improve this answer
    
But IDE shows... The method intValue() is undefined for the type Object –  SWEE Jun 18 '13 at 8:30
1  
Why not use ((Number) userService.getAttendanceList(currentUser)).intValue(); –  johnchen902 Jun 18 '13 at 8:30
    
@user2496295 see my edit, I forgot the Long cast –  fge Jun 18 '13 at 8:31
    
@johnchen902 yes, that is true... But in this case I am not sure OP really wants that. Or that the db model will change from Long to some other numeric type overnight ;) –  fge Jun 18 '13 at 8:33
    
oh... thankyou.. fge.. it's working... but when we write like ((Number) userService.getAttendanceList(currentUser)).intValue(); it is not working why??? –  SWEE Jun 18 '13 at 8:40

The number of results can (theoretically) be greater than the range of an integer. I would refactor the code and work with the returned long value instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.