Home  >  Q&A  >  body text

java - 数组溢出问题,在自己电脑上面不报错,在别人电脑上面报错

我将每条数据进行分割,之后在我的电脑上面运行没有问题,但是把代码迁移到别人的电脑上面就报了数组溢出的 问题。我要进行分割的是这样的数据:nf-000001。以中间的"-"作为分割的界限,把数据分成"nf"和"000001"。
代码

for(int i=1;i<rsRows;i++){
                String originNum=null;
                Cell cell=readsheet.getCell(column,i);
                originNum=cell.getContents();//获得数据“nf-000001”
                String[] numGoods=originNum.split("-");//数据进行分割
                list.add(numGoods[1]);//讲分割的后半部分添加在一个Array<String> list中
            }

错误提示:图片中160行就是报的list.add(numGoods[1])这个语句的错误,我看他说是数组溢出,然而在我的电脑上面没有出现这个问题,很奇怪,求大神相助!!

怪我咯怪我咯2743 days ago485

reply all(4)I'll reply

  • 高洛峰

    高洛峰2017-04-18 10:55:20

    Set a breakpoint. You will know during debugging that the array is out of bounds. Before printing rsRows里面是否有没有-的情况,你在numGoods[1], you should at least check the length of the array for the sake of code robustness.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:55:20

    Too little relevant information given...

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:55:20

    After splitting, judge the length of the numGoods array. It may be that the length of the numGoods element group is less than 2.

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:55:20

    I guess the string entered on this computer is not a string connected by "-".
    You need to see the - symbol clearly. There are shorter ones and longer ones. Make sure you input it correctly

    reply
    0
  • Cancelreply