Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to test some basic hash functions using generic LinkedList and I'm having null pointers

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Trying to test some basic hash functions using generic LinkedList and I'm having null pointers

    Here I have an array of LinkedLists of the string type. I'm trying to store the string in the hash code index of my array but I'm getting a Null Pointer exception when i try to add it. Does this have to do with my casting or am I implementing this whole thing incorrectly. Any help would be greatly appreciated. Here is the code:
    public class Hash {
     
    	public static void main (String[] args){
    		//Scanner input=new Scanner(System.in);
     
    		//System.out.println("Enter your string: ");
    		String s="aaba1xxfsf";
    		int h=Math.abs(s.hashCode())%100000;
    		System.out.println("hash code is: "+h);
    		@SuppressWarnings("unchecked")
    		LinkedList<String>[] list=(LinkedList<String>[])new LinkedList[10000];
    		list[h].add(s);//Null pointer exception happens here
    		for(LinkedList<String> i: list){
    		    if(i!=null)
    			System.out.println("Your string is at index "+i);
     
    		}
    	}
     
    }

  2. The Following User Says Thank You to Nismoz3255 For This Useful Post:

    nrickinedi (November 24th, 2012)


  3. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trying to test some basic hash functions using generic LinkedList and I'm having null pointers

    It all has to do with your creating an array of a reference type but adding no objects to the array, having essentially an array of null. Think of reference arrays as parking spaces in a parking lot. You can't use any cars until you add them them to the lot first.

  4. The Following User Says Thank You to curmudgeon For This Useful Post:

    nrickinedi (November 24th, 2012)

  5. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Trying to test some basic hash functions using generic LinkedList and I'm having null pointers

    I thought I was adding something to the array using list[h].add(s). I figured this would add the first element to that LinkedList and thus the array wouldn't be null at that value in the array. So I have to add a list to the array before I can add an element to the list?? Update: I added this line: list[h]=new LinkedList<String>(); before my add statement and it ran correctly. Thanks for the help. I guess I'm still having trouble with the theory of why it creates a blank array rather than an array of LinkedLists.

  6. The Following User Says Thank You to Nismoz3255 For This Useful Post:

    nrickinedi (November 24th, 2012)

  7. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trying to test some basic hash functions using generic LinkedList and I'm having null pointers

    You are creating an array of object, and it doesn't matter what that object is, nothing will be held by that array except null until you put something in. Doing this, list[h].add(s) doesn't add anything to the array. All it does is add to a linked list that doesn't yet exist, that is not yet held by the array and hence your null.

    On a side note, using a hashCode as an index for an array seems madness to me. Are you sure that you wouldn't rather use a Map instead?

  8. The Following 2 Users Say Thank You to curmudgeon For This Useful Post:

    Nismoz3255 (November 23rd, 2012), nrickinedi (November 24th, 2012)

  9. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Trying to test some basic hash functions using generic LinkedList and I'm having null pointers

    It's for a data structures course and we're using the hash code to place various strings and sub strings into an array of linked lists so we can manipulate the output. It might not be the best example of how to use hash codes, but we have to write the hash function and table ourselves and for whatever reason this is what we have to do. I appreciate the help though.

  10. The Following User Says Thank You to Nismoz3255 For This Useful Post:

    nrickinedi (November 24th, 2012)

Similar Threads

  1. Hash Functions
    By Blueshark in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 03:04 PM
  2. Password Hash - Brute Force Test - SHA-1
    By djl1990 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 14th, 2012, 03:36 PM
  3. From List<String> to a generic LinkedList
    By johnrmsn@Msn.com in forum Collections and Generics
    Replies: 2
    Last Post: July 2nd, 2011, 12:46 PM
  4. Null pointers
    By Jared in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 7th, 2010, 06:40 PM
  5. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM