運行Map Reduce程序時出現NullPointer異常。 請幫助理解爲什麼會出現此錯誤。在執行MapReduce程序時獲取NullPointer異常
public class AvgDriver extends Configured implements Tool{
@Override
public int run(String[] arg0) throws Exception {
Job job=Job.getInstance();
job.setJar("AvgSalary.jar");
job.setMapperClass(AvgMapper.class);
job.setMapOutputKeyClass(NullWritable.class);
job.setMapOutputValueClass(DoubleWritable.class);
//job.setInputFormatClass(TextInputFormat.class);
job.setReducerClass(AvgReducer.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(DoubleWritable.class);
FileInputFormat.setInputPaths(job, new Path(arg0[0]));
FileOutputFormat.setOutputPath(job, new Path(arg0[1]));
return job.waitForCompletion(true)?0:1;
}
public void main(String [] args) throws Exception
{
System.exit(ToolRunner.run(new AvgDriver(), args));
}
}
public class AvgMapper extends Mapper<LongWritable, Text, NullWritable, DoubleWritable> {
public void map(LongWritable key , Text value , Context context) throws IOException, InterruptedException
{
String values=value.toString();
String [] val=values.split("\t");
double convertVal=Double.parseDouble(val[2]);
context.write(NullWritable.get(), new DoubleWritable(convertVal));
}
}
public class AvgReducer extends Reducer<NullWritable, DoubleWritable, NullWritable, DoubleWritable> {
double total=0.0;
int count=0;
public void Reduce(NullWritable key , Iterator<DoubleWritable> value , Context context) throws IOException, InterruptedException
{
while (value.hasNext()) {
total = total+ ((DoubleWritable) value.next()).get();
count++;
}
total=total/count;
context.write(key, new DoubleWritable(total));
}
}
堆棧跟蹤必須指向某行代碼。那是哪一個? – Amit
[cloudera @ quickstart PracticeNew] $ hadoop jar AvgSalary.jar com.ankur.practics.AvgDriver /user/hdfs/empSal.txt/user/hdfs/output15 線程「main」中的異常java.lang.NullPointerException \t at sun .reflect.NativeMethodAccessorImpl.invoke0(本機方法) \t在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) \t在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) \t在java.lang中.reflect.Method.invoke(Method.java:606) \t at org.apache.hadoop.util.RunJar.run(RunJar.java:221) \t at org.apache.hadoop.util.RunJar.main(RunJar的.java:1 36) –
您可以在上面的堆棧跟蹤中看到,沒有類別提及此異常即將到來。 –