HashTable Implemntation

I get a null pointer exception when inserting

Page 1 of 1

9 Replies - 1797 Views - Last Post: 18 March 2010 - 07:58 PM Rate Topic: -----

#1 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

HashTable Implemntation

Posted 18 March 2010 - 06:41 PM

Hey! I'm working on the HashTable implementation, everytime I call my insert method I get a null pointer exception..could anyone give a hint..


public class HashTable
{
    LinkedList[] separateChain;
    int numberObjects = 0;

    //Default constructor of the HashTable
    public HashTable(int size)
    {
       numberObjects = size;
       separateChain = new LinkedList[size];
        for(int j=0; j<size; j++)
            separateChain[j] = null;
    }

    //Return the number of mappings in the map
    public int size()
    {
        return numberObjects;
    }

  //Insert elements into the map
    public void insert(Object o)
    {
        int hash = o.hashCode()%size();
        separateChain[hash].add(o);

    }






Is This A Good Question/Topic? 0
  • +

Replies To: HashTable Implemntation

#2 skibo25  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 12-May 09

Re: HashTable Implemntation

Posted 18 March 2010 - 06:48 PM

You are initializing separateChain to size, which is initialized to numberOfObjects, and are then trying to access the separateChain at hash.

int hash = o.hashCode()%size();


This is not the right way to access the array as it will result in numbers that could be higher than the numberOfObjects if the remainder is higher than the number of objects you have.

This post has been edited by skibo25: 18 March 2010 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: HashTable Implemntation

Posted 18 March 2010 - 06:50 PM

here you make all LinkedList objects null.
separateChain[j] = null; 

anyways, when you declare a new array of LinkedLists, all LinkedLists objects are automatically assigned with a null value, since a LinkedList is of referance data type.

The exception you get is because you attempt to add objects to a null LinkedLists.

you will have to create a new linkedList object in the loop as:
separateChain[j] = new LinkedList(); 

also, when working with a Collection implementation it is better to declare what data type it should hold:
LinkedList<Object> x = new LinkedList<Object>();


perhaps you'll prefer to work with Generics, There is a huge java generics thread pinned.
sorry, guess the tutorial was removed..
here is a tutorial of sun:
http://java.sun.com/...cs-tutorial.pdf

This post has been edited by japanir: 18 March 2010 - 06:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: HashTable Implemntation

Posted 18 March 2010 - 06:58 PM

hey!! thx all

japanir, yours worked I'm not getting null pointer exception anymore..how am I suppose to access the hashCode in the print method..I mean how am I suppose to get the hashValue...

//Prints all the value in the hashTable
    public void print()
    {
        for(int i=0; i < size(); i++)
        {
            System.out.println(separateChain[i]);
        }
    }




apparaently it only printed out the address.....Not sure how am I going to access the hashCode...

THX
Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: HashTable Implemntation

Posted 18 March 2010 - 06:59 PM

skibo25, when the mod operator is applied, it returns the remains of a division between the numbers.
the remains can never be bigger than size, and can never be less than 0 (since he works with non negative values).
so that statement is ok, and won't result with an exception.

anyways, the exception that would have been thrown in such case, is ArrayOutOfBoundsException, and not a NullPointerException.
Was This Post Helpful? 1
  • +
  • -

#6 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: HashTable Implemntation

Posted 18 March 2010 - 07:06 PM

I'm trying to print the data from the student class, however how am I suppose to get the hashCode value in the print method to printout the data from student class...I thought it would work if you just print all the values in the LinkedList array...


public class Student
{
    public static String ssn,dob;
    static String firstName, lastName, address;
    static int size = 10;
    Student next;

    public Student(String getSSN, String getDOB, String getFirst, String getLast, String getAddress)
    {
        ssn = getSSN;
        dob = getDOB;
        firstName = getFirst;
        lastName = getLast;
        address = getAddress;
    }

    @Override public int hashCode()
    {
        return ssn.hashCode();
    }

    @Override public boolean equals(Object o)
    {
        if(this==o)
            return true;
        else
            return false;
    }
}





thx
Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: HashTable Implemntation

Posted 18 March 2010 - 07:14 PM

seperatechain is an array of LinkedList objects.
each value in that array is a LinkedList object.
when you such code:
System.out.println(separateChain[i]);

you invoke the toString() method of the Object stored in that array (in that index). in your case, it is a LinkedList, so the toString() method of a LinkedList is invoked. (it should print the Objects stroed in that LinkedList).
for the hashCode, why not invoke the hashCode method as you have done in the insert method?
Was This Post Helpful? 0
  • +
  • -

#8 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: HashTable Implemntation

Posted 18 March 2010 - 07:24 PM


  //Prints all the value in the hashTable
    public void print()
    {
        int getSSN = ssn.hashCode()%size();
        
        System.out.println(separateChain[getSSN].get(getSSN));
        
    }




I did that, but I get index out of bound..

Hey! thx for your time trying to help...
Was This Post Helpful? 0
  • +
  • -

#9 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: HashTable Implemntation

Posted 18 March 2010 - 07:33 PM

this is what causes the exception:

separateChain[getSSN].get(getSSN)); 

apparently the LinkedList object in index getSSN contains less than the value of getSSN arguments.
I am still not sure what you try to do.

what hashCode you want to print?
as you fill the LinkedList, how do you keep truck on how many Objects you added? and how will you access them?
Was This Post Helpful? 0
  • +
  • -

#10 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: HashTable Implemntation

Posted 18 March 2010 - 07:58 PM

I figure it out..I was trying to print all the data from my class object..

  //Prints all the value in the hashTable
    public void print()
    {
        for(int i= 0; i < numberObjects; i++)
        {
            if(separateChain[i] != null)
            System.out.println(separateChain[i]);
        }
        
    }




Thx for everything
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1