Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:16:56+00:00 2026-06-12T09:16:56+00:00

I am trying to implement in memory sorting,i.e. soring based on the no. of

  • 0

I am trying to implement in memory sorting,i.e. soring based on the no. of counts. I am facing the below problem, null pinter exception in output.collect in the close method of the Reduce class. Pls help!

Is my coding logic correct? I am keeping in memory the tokens from different instances of reduce method. Please help me! I wanted a sorted output of based on the TCounts.

package com.a;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;


public class Map1 extends MapReduceBase implements Mapper<LongWritable, Text, Text, 
Text >{
    public void map(LongWritable key, Text value, OutputCollector<Text, Text> output,
            Reporter reporter) throws IOException {

        StringTokenizer tokenizer = new StringTokenizer(value.toString());

        String tk = tokenizer.nextToken();
        String id = tokenizer.nextToken();
        String name = tokenizer.nextToken();

        StringTokenizer tkz = new StringTokenizer(name, ",");


        ArrayList<String> al = new ArrayList<String>();

        while(tkz.hasMoreTokens())
        {
            name = tkz.nextToken();
            al.add(name);
        }

        for(int i = 0; i<al.size(); i++)
        {
            output.collect(new Text(t+" "+al.get(i)), new Text("1"));
            System.out.println("out key:----->"+t+" "+al.get(i));
        }

    }

}



public class Reduce1 extends MapReduceBase implements Reducer<Text, Text, Text, Text>{
    //  @SuppressWarnings("unchecked")

    ArrayList<TCount> al = new ArrayList<TCount>();
    String key_str = null;
    private OutputCollector<Text, Text> output;



    public void reduce (Text key, Iterator<Text> values, OutputCollector<Text,
            Text> output, Reporter reporter) throws IOException {

        int sum = 0;

        while(values.hasNext())
        {
            String val = values.next().toString();
            sum = sum+Integer.parseInt(val);;

        }

        String str_val = String.valueOf(sum);
        key_str = key.toString();
        //output.collect(key, new Text(str_val));
        TCount tc = new TCount(key.toString(), sum);
        al.add(tc);


    }

    private Text t = new Text();
    private Text txt_key = new Text();


    public void close() throws IOException {
        Collections.sort(al);

        for(int i = 0; i<al.size(); i++)
        {

            String tkn = al.get(i).getT();
            System.out.println("token:-------------------> "+tkn);

            System.out.println("output: "+output);
            txt_key = new Text(t);
            txt = new Text(String.valueOf(al.get(i).getCount()));
            output.collect(txt_key, t);

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:16:56+00:00Added an answer on June 12, 2026 at 9:16 am

    In class Reduce1, you’re declaring the output object:

    private OutputCollector<Text, Text> output; 
    

    without initializing it -> so it’s null at this time.

    Also in the method reduce() you pass a parameter of the same type (OutputCollector<Text, Text> output) -> so in this method, I guess you wanna say:

    this.output=output;
    // if the object is null, initialize it if you wanna use it
    

    The thing is you’ll get a null pointer exception as long as your object is not initialized (instantiated).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a MongoDB / Memory combined Output Cache Provider to use
I'm still learning about Objective-C memory management. I'm trying to implement several simple classes
Trying to implement a search similar to here .This searches properties based on city,locality,property
I am trying to implement a custom memory manager and I am wondering if
I've been trying to implement NSXMLParser and so far I've had memory issues, performance
I have a following problem. I'm trying to implement a model for my QTreeView
I am trying to implement my own memory allocation code which is simple yet
I am trying to implement a decorator chain for my data-access based on IRepository.
I am trying to implement a Memory Mapped File within my application (specifically a
I'm trying to implement the USBPrivateDataSample in my cocoa project. The problem is when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.