为了账号安全,请及时绑定邮箱和手机立即绑定

Hadoop 失败错误:java.lang.ArrayIndexOutOfBounds

Hadoop 失败错误:java.lang.ArrayIndexOutOfBounds

跃然一笑 2023-01-05 16:53:25
我正在使用 Hadoop MapReduce 计算每年的最小值和最大值,但是当我运行该程序时,出现错误:FAILED Error: java.lang.ArrayIndexOutOfBoundsException: 5我认为这是因为我的数据中有空值,因为当没有空值时程序运行正常。因此,在我的 map 函数中,我写了 if 语句来检查是否有 header 以及是否有 null 值: public static class ExposureMapper        extends Mapper<Object, Text, Text, MinMaxExposure> {    private Text year = new Text();    private double minexposure;    private Double maxexposure;    private MinMaxExposure outPut = new MinMaxExposure();    public void map(Object key, Text value, Context context    ) throws IOException, InterruptedException {        try {            //Some condition satisfying it is header            if (value.toString().contains("Product")) {                return;            } else if(value.toString()==null) {               return;            }            else{            }        } catch (Exception e) {            e.printStackTrace();        }        String[] solarFields = value.toString().split(",");        year.set(solarFields[2]);        minexposure = Double.parseDouble(solarFields[5]);        maxexposure = Double.parseDouble(solarFields[5]);        try {            outPut.setMinExposure(minexposure);            outPut.setMaxExposure(maxexposure);            context.write(year, outPut);        } catch (IOException e) {            e.printStackTrace();        }    }但是同样的错误发生了......是不是因为value.toString()==null检查空值的方法不正确?
查看完整描述

1 回答

?
胡子哥哥

TA贡献1825条经验 获得超6个赞

如果value.toString().split(",");has 少于六个元素,solarFields[5]则不会是一个元素,因此您会看到一个ArrayIndexOutOfBoundsException.


创建后solarFields你应该立即检查它的长度:


if (solarFields == null || solarFields.length < 6) {

    return;

}

您还想确保Double.parseDouble(solarFields[5]);不会抛出NumberFormatException:


Double exposure;

try {

    exposure = Double.parseDouble(solarFields[5]);

} catch (NumberFormatException e) {

    return;

}


查看完整回答
反对 回复 2023-01-05
  • 1 回答
  • 0 关注
  • 386 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号