我正在使用 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;
}
添加回答
舉報
0/150
提交
取消