0

I have a problem trying to read the contents in my peakperiod list. The error I have received is java.lang.object cannot be cast to java.lang.String.
Are there any possible workaround for this problem as I am using java 1.6?

Delegate.java

 List peakPeriod = new ArrayList();  
 try{
     peakPeriod = Dao.retrievePeakPeriod("2017");
 for (Iterator i=peakPeriod.iterator(); i.hasNext(); {
     System.out.println("Peak Periods:"+(String)i.next());
 }
 catch(Exception e){ System.out.println("Error:"+e);}

Dao.java

public List retrievePeakPeriod(String year) throws DataAccessException;

DaoImpl.java

public List retrievePeakPeriod(String year) throws DataAccessException {
    List list = getHibernateTemplate().find("select cal.startdate,cal.enddate from Calendar cal " +
            "where to_char(cal.startdate,'yyyy') = ?",
            new Object[] { year },
            new Type[] { Hibernate.STRING });

    return list;
}
3
  • 1
    Don't use raw types. stackoverflow.com/questions/2770321/…. Your query returns a List<Object[]>. So it's quite expected that casting its element to String would fail. Commented Feb 19, 2017 at 9:06
  • Thanks and understood. However, I couldn't use <> for java 1.6 but had to use arraylist to retrieve the results. Commented Feb 19, 2017 at 9:19
  • Generics were introduced in Java 5, in 2004, so 13 years ago. 2 years before the first version of Java 6 came out. So yes, you definitely can use generics in Java 6. Commented Feb 19, 2017 at 9:21

1 Answer 1

1
System.out.println("Peak Periods:"+ i.next());

You don't have to cast it to a String - java will automatically call the toString() method, so the above is equivalent to:

System.out.println("Peak Periods:"+ i.next().toString());

If your list can have nulls, you can do something safer such as:

System.out.println("Peak Periods:"+ String.valueOf(i.next()));

Edit: This assumes that the object returned has a useful toString representation and it's what you want. Otherwise, you'd need to figure out the type (class) of (the) object that's returned and do with it what you want.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the swift reply. I have tried the 3 above solution but the result are similar. Peak Periods:[Ljava.lang.Object;@40dc76dd Peak Periods:[Ljava.lang.Object;@49ffc86f Peak Periods:[Ljava.lang.Object;@37c0155d Peak Periods:[Ljava.lang.Object;@23baa943 Peak Periods:[Ljava.lang.Object;@33be8008
@JanosFang Which shows what I'm telling you in my comment: your query returns a List<Object[]>, not a List<String>. How could two fields (cal.startdate and cal.enddate) be stored in a single String?
@JBNizet Thanks JBNizet for identifying the cause. It was my blunder that did not took the parameters into consideration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.