Hadoop失败错误:java.lang.ArrayIndexOutOfBoundsException:5

时间:2019-06-06 15:14:18

标签: java hadoop

我正在使用Hadoop MapReduce计算每年的最小值和最大值,但是当我运行该程序时,出现错误:root = tk.Tk() #-----------------------------------CONFIG------------------- root.overrideredirect(True) root.lift() root.wm_attributes("-disabled", True) root.wm_attributes("-transparentcolor", "gray") root.geometry("+10+670") #-----------------------------------Labels------------------- text1 = "Text1: " + "this is tryal command. It was used only to understand style of a window." text2 = "Text2: " + "this is tryal command. It was used only to understand style of a window. White space above" lbl1 = tk.Label(root, text=text1, font='Arial 15', fg="Red", bg='gray') lbl2 = tk.Label(root, text=text2, font='Arial 15', fg="Red", bg='gray') lbl1.pack() lbl2.pack() #---------------------------------------------------------- lbl1.mainloop()

我认为这是因为我的数据中包含空值,因为当没有空值时程序运行正常。

因此,在我的map函数中,我编写了if语句来检查是否存在标题和空值:

FAILED Error: java.lang.ArrayIndexOutOfBoundsException: 5

但是发生同样的错误... 是因为 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 个答案:

答案 0 :(得分:1)

如果value.toString().split(",");的元素少于六个,则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;
}