我在运行Map Reduce Program时遇到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));
}
}
答案 0 :(得分:1)
您在main方法中缺少静态。更新如下。
public static void main(String [] args) throws Exception