2016-10-03 56 views
-1

運行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)); 
    } 
} 
+0

堆棧跟蹤必須指向某行代碼。那是哪一個? – Amit

+0

[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) –

+0

您可以在上面的堆棧跟蹤中看到,沒有類別提及此異常即將到來。 –

回答

1

您在main方法缺少靜態。更新如下。

public static void main(String [] args) throws Exception 
+0

我做了另一個程序,其中Job job = Job.getInstance()工作正常。 –

+0

你試過這個嗎?它是否解決了這個問題?在你得到空指針的地方,你現在將從堆棧跟蹤中獲得特定的java行,請張貼它。 –

+0

我嘗試過但仍然是相同的錯誤。 –