grails&groovy的IllegalArgument异常

我在开发的过程中遇到了这样一个异常,总是提示IllegalArgument异常,代码大致如下:

 1 if(haomgl.save(flush:true)){
 2             
 3    //更新库存:状态为2的位置存煤
 4    def cunmInfoOfWeiz = null;
 5    def cunmInfoListOfWeiz = getCunmInfoListOfWeiz(haomgl.getMixunit().id, haomgl.getStoreposition(), haomgl.getCoaltype().id);
 6    if(cunmInfoListOfWeiz.size() > 0){
 7         cunmInfoOfWeiz = Jinmgl.get(Integer.parseInt(String.valueOf(cunmInfoListOfWeiz.get(0).id.intValue())));
 8                 
 9         .....10    } else { ... } 

代码在执行到7行时调用get方法时总是抛出这个IllegalArgument异常,查了好久以为是实体类Domain中存在字段为空,所以报这个错,后来发现,将4、5行代码改成下面的定义就可以了,

Jinmgl cunmInfoOfWeiz = null;
List cunmInfoListOfWeiz = getCunmInfoListOfWeiz(haomgl.getMixunit().id, haomgl.getStoreposition(), haomgl.getCoaltype().id);

把相应的对象不用def去定义,而是直接使用它的类型背身定义异常就不会出现了。

另一种原因就是你的数据有null的。

原文地址:https://www.cnblogs.com/smallrock/p/3612179.html