java.lang.ClassCastException:无法强制转换为java.lang.Long

时间:2016-06-14 15:28:18

标签: java list collections long-integer

  

块引用

我正在尝试迭代Long对象列表并将每个值设置为长变量。但是我得到了

  

java.lang.ClassCastException ::无法强制转换为java.lang.Long

如何解决此问题?

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)

2 个答案:

答案 0 :(得分:0)

最可能代码中的场景是这样的:

    class `your_com.sample`{ 
    long a;..... 
    getA(); .--getters & setters
   setA();
}

现在,您需要在代码中的所有位置使用list<Long>声明替换所有list<your_com.sample>

此外,您的地图声明将如下所示:

final Map<String, your_com.sample> out = simpleJdbcCall.execute(params);

完成所有这些替换后,您需要返回your_com.sample

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();
}

答案 1 :(得分:0)

考虑您的方法SampleService.sampleList()。它返回的内容不是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);
    }

这可能会有所帮助。

<强>结果:

ArrayList元素 10000