java.math.BigInteger cannot be cast to java.lang.Long

133,348

Solution 1

Your error might be in this line:

List<Long> result = query.list();

where query.list() is returning a BigInteger List instead of Long list. Try to change it to.

List<BigInteger> result = query.list();

Solution 2

Better option is use SQLQuery#addScalar than casting to Long or BigDecimal.

Here is modified query that returns count column as Long

Query query = session
             .createSQLQuery("SELECT COUNT(*) as count
                             FROM SpyPath 
                             WHERE DATE(time)>=DATE_SUB(CURDATE(),INTERVAL 6 DAY) 
                             GROUP BY DATE(time) 
                             ORDER BY time;")
             .addScalar("count", LongType.INSTANCE);

Then

List<Long> result = query.list(); //No ClassCastException here  

Related link

Solution 3

Try to convert the BigInteger to a long like this

Long longNumber= bigIntegerNumber.longValue();

Solution 4

I'm lacking context, but this is working just fine:

List<BigInteger> nums = new ArrayList<BigInteger>();
Long max = Collections.max(nums).longValue(); // from BigInteger to Long...

Solution 5

It's a very old post, but if it benefits anyone, we can do something like this:

Long max=((BigInteger) Collections.max(dynamics)).longValue(); 
Share:
133,348

Related videos on Youtube

Tony
Author by

Tony

My favorite programming language is Java with Spring Framework.

Updated on March 07, 2022

Comments

  • Tony
    Tony about 2 years

    I've got List<Long> dynamics. And I want to get max result using Collections. This is my code:

    List<Long> dynamics=spyPathService.getDynamics();
            Long max=((Long)Collections.max(dynamics)).longValue(); 
    

    This is my getDynamics:

    public List<Long> getDynamics() {
    
            Session session = null;
    
            session = this.sessionFactory.getCurrentSession();
            Query query = session
                    .createSQLQuery("SELECT COUNT(*) FROM SpyPath WHERE DATE(time)>=DATE_SUB(CURDATE(),INTERVAL 6 DAY) GROUP BY DATE(time) ORDER BY time;");
    
            List<Long> result = query.list();
            return result;
    
        }
    

    Now I'm getting java.math.BigInteger cannot be cast to java.lang.Long. What's wrong?

    • Brian Roach
      Brian Roach over 10 years
      I'd say you're trying to cast a BigInteger to a Long.
    • Luiggi Mendoza
      Luiggi Mendoza over 10 years
      Because a java.math.BigInteger class instance is not an instance of java.lang.Long class.
    • Eric Stein
      Eric Stein over 10 years
      You're going to need to provide more code if you want more help. Maybe the line the error is pointing to? And what type does getDynamics() return?
    • user902383
      user902383 over 10 years
      you have your answer in your question, i assume this Collections.max(dynamics)) is BigInteger, and you are trying cast it to long, try to cast it to BigInteger, and then use longValue()method
    • StormeHawke
      StormeHawke over 10 years
      Ok people, he's clearly labeled his dynamics list as a List<Long>. @Tony Check the return type of spyPathService.getDynamics(). Assuming your error is actually coming from these lines of code, I would guess from this code that spyPathService.getDynamics() is actually returning a List that at the very least includes some BigIntegers
  • Tony
    Tony over 10 years
    I heard that COUNT(*) statement returns Long. Doesn't it?
  • mike
    mike over 10 years
    As @StormeHawke said '...is actually returning a List that at the very least includes some BigIntegers'. So you better find out exactly, before you run into the next problem of this kind.
  • gavenkoa
    gavenkoa about 10 years
    This answer lies on wrong assumption about internal Hibernate wrapper types! The only Aniket Kulkarni answer SQLQuery#addScalar is right!
  • gavenkoa
    gavenkoa about 10 years
    You are hero! +1, but really +100!
  • Bills
    Bills over 6 years
    I also type cast the list