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 9169295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:50:43+00:00 2026-06-17T15:50:43+00:00

class Item { private int address; private String itemString; public Item(String item) { separate(item);

  • 0
class Item
{
    private int address;
    private String itemString;

    public Item(String item)
    {
        separate(item);
    }

    public void separate(String string)
    {
        StringTokenizer st = new StringTokenizer(string);
        itemString = st.nextToken();
        if(st.hasMoreTokens())
        {
            address = Integer.parseInt(st.nextToken());
        }
        else
        {
            address = -1;
        }
    }

    public String getKey()
    {
        return itemString;
    }

    public int getAddress()
    {
        return address;
    }

    public void illegitimize()
    {
        itemString = "*del";
        address = -1;
    }
}

class HashTable
{
    private Item[] hashArray;
    private int arraySize;

    public HashTable(int size)
    {
       arraySize = size;
       hashArray = new Item[arraySize];
    }

    public int hash(Item item)
    {
        String key = item.getKey();

        int hashVal = 0;
        for(int i=0; i<key.length(); i++)
        {
            int letter = key.charAt(i) - 96;
            hashVal = (hashVal * 26 + letter) % arraySize;
        }

        return hashVal;
    }

    public void insert(Item item)
    {
        int hashVal = hash(item);

        while(hashArray[hashVal] != null && 
                !(hashArray[hashVal].getKey().contains("*")))
        {
            hashVal++;
            hashVal %= arraySize;
        }

          String keyAtHashVal = hashArray[hashVal].getKey();
          String itemKey = item.getKey();

        if(!keyAtHashVal.equals(itemKey))
        {
            hashArray[hashVal] = item;
            System.out.println(item.getKey() + " inserted into the table at "
                    + "position " + hashVal);
        }
        else
        {
            System.out.println("Error: " + item.getKey() + " already exists "
                    + "at location " + hashVal);
        }
    }

    public Item find(Item item)
    {
        int hashVal = hash(item);

        while(hashArray[hashVal] != null)
        {
            if(hashArray[hashVal].getKey().equals(item.getKey()))
            {
                System.out.println(item.getKey() + " found at location "
                        + hashVal + " with address " + item.getAddress());
                return hashArray[hashVal];
            }

            hashVal++;
            hashVal %= arraySize;
        }

        System.out.println("Error: " + item.getKey() + " not found in the "
                + "table");
        return null;
    }

}

public class HashTableMain
{
    public static void main(String[] args) throws Exception
    {
        File file = new File(args[0]);
        Scanner input = new Scanner(file);

        Item currentItem;
        String currentItemsKey;
        int currentItemsAddress;

        HashTable table = new HashTable(50);

        while(input.hasNextLine())
        {
            currentItem = new Item(input.nextLine());
            currentItemsKey = currentItem.getKey();
            currentItemsAddress = currentItem.getAddress();

            if(currentItemsAddress > 0)
            {
                table.insert(currentItem);
            }
            else
            {
                table.find(currentItem);
            }
        }
    }
}

The title pretty much explains it. I get a null pointer when the insert() method attempts to retrieve the key of the first item I feed it from the file. I figure this has something to do with the way I retrieve store the string but I cannot identify the problem.

The records inside the file will be in this format:
george
stacy 112
patrick 234
angelo 455
money 556
kim
chloe 223

If there is a number in the line I need to hash the item into the array at the appropriate location. If there is no number I need to search for the key (the string at the beginning of each line).

Edit: added find function. I left out anything I didn’t think you needed to help me. If you need anything else let me know.

  • 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-17T15:50:44+00:00Added an answer on June 17, 2026 at 3:50 pm

    The problem seems to be at

    String keyAtHashVal = hashArray[hashVal].getKey();
    

    in the HashTable.insert() . Your hashArray[hashVal] may not have an object in it leading to a null pointer. You could do a null check.

    Item existingItem =  hashArray[hashVal];
    if(existingItem==null) {
     //do appropriate code
    } else {
     //do your stuff
    }
    

    BTW, StringTokenizer is deprecated and is only there for compatibility purposes. You could use the String.split() method.
    Plus instead of HashTable , you can use the HashMap if you are not aware of it

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

Sidebar

Related Questions

This is my Item class: public class Item { private String id; private int
My base class: class Item { protected: int count; string model_name; int item_number; public:
The setup: class Item { private int _value; public Item() { _value = 0;
Given the class: public class Item { [Key] public int ID { get; set;
I have some custom type: [RdfSerializable] public class Item { [RdfProperty(true)] public string Name
I have these two classes @DatabaseTable public class User { @DatabaseField(generatedId=true) private int _id;
I have class for iterating images. public class PictureManager { private int _current; public
I have a custom class class RouteStop { public int number; public string location;
class Venue { private int id; private String name; } I populate the ListView
I've got a very simple object: public class DocumentType { private int id; private

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.