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

  • SEARCH
  • Home
  • 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 7629173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:46:01+00:00 2026-05-31T05:46:01+00:00

I built a symbol table using an array list filled with an object pairs

  • 0

I built a symbol table using an array list filled with an object “pairs” that are singly-linked chains and hold a word and the number of times it occurs in a text file. I need to use this for the FrequencyCounter program that counts the number of words in a file. For some reason, I’m getting this error when running the the FrequencyCounter with the HashST:

Processed 1215985 words (19 sec; 19710 msec
Exception in thread "main" java.lang.NullPointerException

at HashST.hashIndex(HashST.java:60)
at HashST.get(HashST.java:105)
at FrequencyCounter.main(FrequencyCounter.java:112)

I have an idea that there’s something wrong with my HashST and its not putting the pairs in the ArrayList like I wanted it to. Any suggestions on what is wrong with the implementation would be greatly appreciated.

Here is my code and the code for the FrequencyCounter:

import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Iterator;

public class HashST<Key extends Comparable<Key>, Value> implements Iterable<Key> {
private ArrayList<Pair> chains;
private int numKeys;
private int numChains;


public class Pair
{
    Key key;
    Value value;
    Pair(Key k, Value v)
    {
        key = k;
        value = v;
    }
    Pair()
    {}

    Pair next;
}

    /**
 * Initialize an empty HashSt with a default of 64 empty chains.
 */
    public HashST()
    {
        this(64);

    }

/**
 * Initialize an empty HashST with numChains emptychains.
 * 387911 is a prime number about twice the number of distinct
 * words in the leipzig1M.txt file.
 */
public HashST(int numChains)
{
    this.numChains = numChains;
    chains = new ArrayList<Pair>();

    for(int i = 0; i < numChains; i++)
    {
        Pair p = new Pair(null, null);
        chains.add(p);

    }
}

/**
 * compute the hash index for a key k if the number of
 * chains is N
 */
private int hashIndex(Key k, int N)
{
    return (k.hashCode() & 0x7fffffff) % N;

}
/**
 * insert the Pair (k,v) into the appropriate chain and increment 
 * the number of keys counter or 
 * update the value for k if k is already in the hash table.
 *
 */
public void put(Key k, Value v) {

    int i = hashIndex(k, numChains);
    Pair tmp = chains.get(i);
    if(contains(k))
    {
        while(tmp.next != null)
        {
            if(tmp.key == k)
            {
                tmp.value = v;
                return;
            }

            tmp = tmp.next;
        }
    }
    else
    {
        Pair p = new Pair(k, v);
        tmp.next = p;
        numKeys ++;

    }




}

/**
 * return the value for key k if it is in the hash table
 * or else return null if it is not.
 */
public Value get(Key k) {

    int i = hashIndex(k, numChains);
    Pair tmp = chains.get(i);
        while(tmp.next != null)
        {
            if(tmp.key == k)
            {
                return tmp.value;

            }

            tmp = tmp.next;
        }


    return null;

}

/**
 * remove the pair with key k if it is in the hash table
 * otherwise no change.
 */
public void delete(Key k) {

    if(contains(k))
    {
        return;


    }

}

/**
 * return true if the hash table contains a pair with key 
 * equal to k else return false
 */
public boolean contains(Key k) {

    return (get(k) != null) ? true : false;

}

/**
 * return the number of keys in the hash table
 */
public int size() {

    return numKeys;

}

/**
 * return a LinkedList<Key> containing the keys in the
 * hash table
 */
public Iterable<Key> keys() {

    LinkedList<Key> l = new LinkedList<Key>();
    for(Pair p : chains)
    {
        while(p.next != null)
        {
            l.add(p.key);
            p = p.next;
        }

    }


    return l;
}

/**
 * return an Iterator<Key> for the keys in the hash table
 */ 
public Iterator<Key> iterator() {
    return keys().iterator();
}

}

And here is the Frequency Counter: http://algs4.cs.princeton.edu/31elementary/FrequencyCounter.java.html

  • 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-05-31T05:46:02+00:00Added an answer on May 31, 2026 at 5:46 am

    According to your stack trace this would seem to be the line that threw a null pointer:

    return (k.hashCode() & 0x7fffffff) % N;
    

    So we have one object reference k, an integer constant, and a primitive N. Neither the constant nor the primitive can be null, the only thing being dereferenced here is k. So it looks like someone tried to get a value for a null k!

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

Sidebar

Related Questions

Using MSSQL 2008. I looked around a bit online for a table that maps
I built a Winform app several months ago that schedules appointments for repair techs.
I built a template-based document generator using the Open XML SDK (1.0) , the
I built a client server application using posix shared memory and posix unnamed semaphores
Is there a built in linux utility that I can use to test a
I've built a static library, to be linked in my iPhone apps. This library
I have built a variety of little scripts using Ruby's very simple Queue class,
Language: C# I have to pass a huge string array (built dynamically) as an
I have to define a large array (lookup table) in a code. It contains
I have built the OpenCV libraries using the cmake build system as described here

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.