Closed. This question needs debugging details。它当前不接受答案。
想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。
4年前关闭。
Improve this question
运行Map Reduce程序时出现NullPointer异常。
请帮助了解为什么会出现此错误。
想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。
4年前关闭。
Improve this question
运行Map Reduce程序时出现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));
}
}
最佳答案
您在主要方法中缺少静态。更新如下。
public static void main(String [] args) throws Exception
关于java - 执行MapReduce程序时出现NullPointer异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39838579/
10-16 03:01