Blockquote
I am trying to Iterate List of Long objects and setting each value into long variable.But i am getting
java.lang.ClassCastException :: cannot be cast to java.lang.Long
How can I get solve this issue?
List<Long> listLong = new ArrayList<Long>();
listLong = SampleService.sampleList();
for(int i=0;i<listLong.size();i++){
long sampleId = listLong.get(i).longValue();
}
`
public List<Long> sampleList() throws Exception{
LOGGER.info("start of sampleList method ");
List<Long> sampList = null;
final MapSqlParameterSource params = new MapSqlParameterSource();
final SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate)
.withProcedureName("SAMPLE_TERM");
simpleJdbcCall.declareParameters(new SqlOutParameter("SAMPLE_CURSER",
OracleTypes.CURSOR, new SampleRowMapper()));
final Map<String, Object> out = simpleJdbcCall.execute(params);
sampList = (List<Long>) out.get("SAMPLE_CURSER");
LOGGER.debug("List :: "+sampList .size());
return sampList ;
}
java.lang.ClassCastException: com.sample cannot be cast to java.lang.Long
at com.sample.cronTask(sample.java:49)
at sun.reflect.GeneratedMethodAccessor45.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Most Probably the scenario in code is like this:
class `your_com.sample`{
long a;.....
getA(); .--getters & setters
setA();
}
now, You need to replace all list<Long>
with list<your_com.sample>
declarations at all places in code.
Also, your Map declarations would be like this:
final Map<String, your_com.sample> out = simpleJdbcCall.execute(params);
Once you do all those replacements, then for return of your_com.sample
you need to do below:
List<your_com.sample> listLong = new ArrayList<your_com.sample>();
listLong = SampleService.sampleList();
for(int i=0;i<listLong.size();i++){
long sampleId = listLong.get(i).getA();
}
Consider your method SampleService.sampleList(). It returns something which is not of type Long.
private static List<Long> sampleList(){
long[] longs = new long[]{1L};
longs[0]=10000;
List<Long> myList = new ArrayList<Long> ();
for (long item : longs)
myList.add(item);
return myList;
}
public static void main(String[] args) {
List<Long> listLong = new ArrayList<Long> ();
listLong=sampleList();
System.out.println(" ArrayList Elements");
for(int i=0;i<listLong.size();i++){
long sampleId = listLong.get(i).longValue();
System.out.println(sampleId);
}
This might be helpful.
Result:
ArrayList Elements 10000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.