0

I am implementing Hash Table through java. When I implement the following code, I get a NullPointer Exception error but if I replace the else statement in void put(int key, int value) in class HashTable with the statement kv[key]=new KeyValue(key,value); It works! Can someone explain why is this happening? Please help!

public class KeyValue {
    int key;
    int value;

    KeyValue(int k, int v)
    {
        key = k; 
        value = v;
    }
    public int getKey(){
        return key;
    }

    public int getValue()
    {
        return value;
    }

    public void put(int k, int v)
    {
        key = k;
        value = v;
    }

}

public class HashTable{

    KeyValue[] kv;

        HashTable(){
            kv = new KeyValue[4];
            for (int i=0; i<4 ; i++)
            {    kv[i]=null;

            }
        }

    void put(int key, int value)
        {
            if((kv[key]!=null) || (key<0 && key>=4))
            {
                ;
            }
            else
            {
                kv[key].put(key,value);
            }
        }

        int get(int key)
        {
            int value;

            value=kv[key].getValue();


            return value;
        }

    public static void main(String[] a){

            HashTable h = new HashTable();
            h.put(1,2);

            System.out.println(h.get(1));

        }

}
2
  • My comment became too long, so I posted it as an answer. Commented Apr 15, 2014 at 10:24
  • This is not a hash table. It is basically an array-wrapper. Commented Apr 15, 2014 at 10:27

2 Answers 2

6

You are initializing kv[key] with null. So if you do kv[key].callSomeMethod you got an NullPointerException because there is no object to call a method of.

You can initialize your KeyValues like Peter Radar said or do it in the constructor of your HashTable using the default constructor of KeyValue.

    HashTable(){
        kv = new KeyValue[4];
        for (int i=0; i<4 ; i++)
        {    
            kv[i]=new KeyValue();
        }
    } 

Now an Object of type KeyValue is stored in kv[i] and you can use its methods.

Sign up to request clarification or add additional context in comments.

1 Comment

In other words: new KeyValue[4] initializes the array, but it just creates it filled with nulls, any single object must be initialized
1

To answer your question I believe kai and Peter Rader found the issue. But there are other things in your code that I recommend to change.

The KeyValue.put method should be renamed to set or completely dropped. The basic question is here if KeyValue should be a mutable data object or not.

In HashTable.put the your code if((kv[key]!=null) || (key<0 && key>=4)) should read if ((kv[key]!=null) || key<0 || key>=4) (besides you should of course replace 4 with a variable so you can have larger HashTables too).

Since you are trying to learn something from this I'd also recommend that you try to replace KeyValue[] kv; with a Collection.

3 Comments

Thanks! I was trying to implement something very basic so that's why I used 4. Thanks for the advice though. Also ((kv[key]!=null) || key<0 || key>=4) doesn't solve my purpose as I am checking that whether the user enters a key within this range (key<0 && key>=4) or not. Because all the keys are defined in this range so it doesn't work for a key entered by the user that doesn't exist. BTW who is Peter Radar? I can't see his comment/answer, I can only see kai's answer!!!
@Meha: But key<0 && key>=4 can never be true! key can not at the same time be smaller than 0 and larger than 4 :) Its key<0 || key>=4 you are looking for. The answer by Peter seems to be retired.
Its this (key>0 && key<=4) what I was wanting to do :D ... there was a typo. And what you said can also be done.. its the same thing. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.