sozhen
sozhen

Reputation: 7677

Hadoop NullPointerException in my mapper class

Please help:

In my mapper class I have instance variable protected transient HashMap<String, Double> _map = null;

I initialized this variable in my setup(Context context) method, also _map is populated with data read in from a SequenceFile.

setup emthod:

@Override
    protected void setup(Context context) throws IOException, InterruptedException 
    {       
        super.setup(context);

        Configuration conf = context.getConfiguration();

        _map = new HashMap<String, Double>();

        Path seqFilePath = new Path(conf.get("in"));
        Reader reader;

        try 
        {
            reader = new Reader(conf, Reader.file(seqFilePath));
            Text key = new Text();
            DoubleWritable value = new DoubleWritable();
            while (reader.next(key, value)) 
            {
                _map.put(key.toString().trim(), value.get());
            }
        }
        catch (IOException e) 
        {
            LOGGER.error("Can't find the input path to read: " + seqFilePath, e);
        }
    }

map() method:

@Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
    {
        ...
        getDiscretizationLabel(...);
        ...
    }

In my getDiscretizationLabel(...) method I try to retrieve data from _map variable but a NullPointerException is throwing:

private void getDiscretizationLabel(String attribute, String value, String category, int bin, Context context) throws IOException, InterruptedException 
    {
        ...
        min = _map.get(attribute + "_min"); // throws NullPointerException
        max = _map.get(attribute + "_max");

        ...

    }

getDiscretizationLabel(...) throws the NullPointerException, so far I am not able to figure out why is that and is blocked here.

Is there a way to resolve this or a workaround? Thanks!

Upvotes: 0

Views: 170

Answers (1)

Chris Gerken
Chris Gerken

Reputation: 16392

My guess is that the file isn't being loaded/found correctly. Just as an aside, I would use a counter (group="error", name="IOException") to count the number of times the IOException is thrown in the setup() method. It's really easy to see that count in the counter report:

context.getCounter("error","IOException").increment(1); 

If you're sure that the error isn't being thrown, write an error to the logger before the try-catch block. Use a severity of error so that you can confirm that you can find logged error messages.

Upvotes: 2

Related Questions