null pointer exception in getstrings method hadoop

210 views Asked by At

Getting Null pointer exception in Driver class conf.getstrings() method. This driver class is invoked from my custom website.

Below are Driver class details

 @SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request,
                 HttpServletResponse response)
         throws ServletException, IOException
{
       Configuration conf = new Configuration();       
       //conf.set("fs.default.name", "hdfs://localhost:54310");
       //conf.set("mapred.job.tracker", "localhost:54311");
       //conf.set("mapred.jar","/home/htcuser/Desktop/ResumeLatest.jar");
       Job job = new Job(conf, "ResumeSearchClass");        
       job.setJarByClass(HelloForm.class);      
       job.setJobName("ResumeParse");
       job.setInputFormatClass(FileInputFormat.class);
       FileInputFormat.addInputPath(job, new Path("hdfs://localhost:54310/usr/ResumeDirectory"));
       job.setMapperClass(ResumeMapper.class);
       job.setReducerClass(ResumeReducer.class);
       job.setMapOutputKeyClass(IntWritable.class);
       job.setSortComparatorClass(ReverseComparator.class);       
       job.setMapOutputValueClass(Text.class);
       job.setOutputKeyClass(IntWritable.class);
       job.setOutputValueClass(Text.class);
       job.setOutputFormatClass(FileOutPutFormat.class);
       FileOutputFormat.setOutputPath(job, new Path("hdfs://localhost:54310/usr/output" + System.currentTimeMillis()));
       long start = System.currentTimeMillis();
       var = job.waitForCompletion(true) ? 0 : 1;

Getting NULL pointer exception from following two line of code

String[] keytextarray=conf.getStrings("Keytext");   

for(int i=0;i<keytextarray.length;i++) //GETTING NULL POINTER EXCEPTION HERE IN keytextarray.length
    {
        //some code here
    }

       if(var==0)
       {        
        RequestDispatcher dispatcher = request.getRequestDispatcher("/Result.jsp");
        dispatcher.forward(request, response);
       long finish= System.currentTimeMillis();
       System.out.println("Time Taken "+(finish-start));
       }
}

I have removed few unwanted codes from above Drives class method...

Below are RecordWriter class where I use conf.setstrings() in Write() method to set values

Below are RecordWriter class details

    public class RecordWrite extends org.apache.hadoop.mapreduce.RecordWriter<IntWritable, Text> {

    TaskAttemptContext context1;
    Configuration conf;

    public RecordWrite(DataOutputStream output, TaskAttemptContext context)
    {
        out = output;        
        conf  = context.getConfiguration();
        HelloForm.context1=context;        
        try {
            out.writeBytes("result:\n");
            out.writeBytes("Name:\t\t\t\tExperience\t\t\t\t\tPriority\tPriorityCount\n");            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public RecordWrite() {
        // TODO Auto-generated constructor stub
    }
    @Override
    public void close(TaskAttemptContext context) throws IOException,
            InterruptedException
    {    
        out.close();
    }
    int z=0;
    @Override
    public void write(IntWritable value,Text key) throws IOException,
            InterruptedException
    {

        conf.setStrings("Keytext", key1string); //setting values here        
        conf.setStrings("valtext", valuestring);
        String[] keytext=key.toString().split(Pattern.quote("^"));
        //some code here

    }
}`

`I suspect this null pointer exception happens since i call conf.getstrings() method after job is completed (job.waitForCompletion(true)). Please help fix this issue.

If above code is not correct way of passing values from recordwriter() method to driverclass.. please let me know how to pass values from recordwriter() to driver class.

I have tried option of setting values in RecordWriter() to an custom static class and accessing that object from static class in Driverclass again returns Null exception if i am running code in cluster..

1

There are 1 answers

9
Ramzy On

If you have the value of key1staring and valuestirng, in Job class, try setting them in job class itself, rather than RecordWriter.write() method.