亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 354 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號